依赖类型通常需要使用typename
告诉编译器该成员是一个类型,而不是函数或变量。
然而,并非总是这样。
例如,基类不需要这样做,因为它只能是一个类型:
template<class T> struct identity { typedef T type; }
template<class T> class Vector : identity<vector<T> >::type { }; // no typename
现在我的问题是,为什么
typedef
后面有时需要加上typename
?关于这个问题,请看下文解答。template<class T> class Vector
{
typedef typename /* <-- why do we need this? */ vector<T>::iterator iterator;
};
static
的同样的问题呢? - n. m.static x;
默认会是int
类型,不是吗?(或者至少在C语言中是这样的吧?)但说实话,我不确定,这是个好问题。 - user541686