且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 enable_if 检查成员是否存在

更新时间:2023-11-27 18:49:28

这在 C++11 中变得方式更容易.

This has become way easier with C++11.

template <typename T> struct Model
{
    vector<T> vertices;

    void transform( Matrix m )
    {
        for(auto &&vertex : vertices)
        {
          vertex.pos = m * vertex.pos;
          modifyNormal(vertex, m, special_());
        }
    }

private:

    struct general_ {};
    struct special_ : general_ {};
    template<typename> struct int_ { typedef int type; };

    template<typename Lhs, typename Rhs,
             typename int_<decltype(Lhs::normal)>::type = 0>
    void modifyNormal(Lhs &&lhs, Rhs &&rhs, special_) {
       lhs.normal = rhs * lhs.normal;
    }

    template<typename Lhs, typename Rhs>
    void modifyNormal(Lhs &&lhs, Rhs &&rhs, general_) {
       // do nothing
    }
};

注意事项:

  • 您可以在 decltypesizeof 中命名非静态数据成员,而无需对象.
  • 您可以申请扩展的 SFINAE.基本上可以检查任何表达式,如果在替换参数时它无效,则模板将被忽略.
  • You can name non-static data members in decltype and sizeof without needing an object.
  • You can apply extended SFINAE. Basically any expression can be checked and if it is not valid when the arguments are substituted, the template is ignored.