您的位置:首页 > 其它

typedef typename 是什么东东

2015-01-26 17:50 453 查看
最近在阅读STL 源码的时候发现 

  typedef typename _Rep_type::pointer pointer; 网上找了下资料,转载如:1.template<typename T>
2.class A {
3.public:
4. typedef T a_type;
5.};
6.template<typename A>
7.class B {
8.public:
9. //typedef A::a_type b_type;
10. typedef typename A::a_type b_type;
11.};
12.int main() {
13. B<A<int>> b;
14. return 0;
15.}

template<typename T>
class A {
public:
typedef T a_type;
};
template<typename A>
class B {
public:
//typedef A::a_type b_type;
typedef typename A::a_type b_type;
};
int main() {
B<A<int>> b;
return 0;
}


如果把注释取消,就会产生编译错误。

必须使用关键字typename的原因是T是一个template parameter。在实例化之前,编译器对T一无所知,因此不知道A::a_type 代表的是一个type或是一个member function或是一个data member,使用typename可以告诉编译器这是一个type使得编译能顺利通过
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: