相关类型通常需要typename
来告诉编译器成员是一个类型,而不是函数或变量。为什么'typedef'这个单词在依赖类型后需要'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 .:哦,对不起,我现在明白了。是的,我没有想到这一点,但我认为这可能是因为默认情况下int ...默认情况下...所以'静态x;'将默认是'int'不是吗? (或者至少它会在C?)但是老实说不知道,这是一个很好的问题。 – Mehrdad
大约15年前,C已经下降了隐式int :) – hvd