【发布时间】:2014-02-07 10:22:58
【问题描述】:
依赖类型通常需要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 中?)但老实说不确定,这是一个好问题。 -
C 在大约 15 年前也放弃了隐式 int :)