考虑以下代码:
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"的情况?
这是我正在尝试做的事情:
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这个问题,或者甚至可以应用它.