相关疑难解决方法(0)

什么时候需要"typename"关键字?

可能重复:
正式,什么是typename?
我必须在哪里以及为什么要放置模板和typename关键字?

考虑以下代码:

template<class K>
class C {
    struct P {};
    vector<P> vec;
    void f();
};

template<class K> void C<K>::f() {
    typename vector<P>::iterator p = vec.begin();
}
Run Code Online (Sandbox Code Playgroud)

为什么此示例中需要"typename"关键字?是否还有其他必须指定"typename"的情况?

c++ syntax templates typename

50
推荐指数
3
解决办法
3万
查看次数

使用enable_if检查成员是否存在

这是我正在尝试做的事情:

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

    #if T has a .normal member
    void transform( Matrix m )
    {
        each vertex in vertices
        {
          vertex.pos = m * vertex.pos ;
          vertex.normal = m * vertex.normal ;
        }
    }
    #endif

    #if T has NO .normal member
    void transform( Matrix m )
    {
        each vertex in vertices
        {
          vertex.pos = m * vertex.pos ;
        }
    }
    #endif
} ;
Run Code Online (Sandbox Code Playgroud)

我已经看过使用的例子enable_if,但是我无法理解如何应用enable_if这个问题,或者甚至可以应用它.

c++ templates sfinae

29
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×2

templates ×2

sfinae ×1

syntax ×1

typename ×1