typename

来自cppreference.com
跳转到: 导航, 搜索


[编辑] 指定模板的类型参数

template 的参数列表中 typename class 是可以互换使用的,用来指定类型参数。虽然它们对编译器来讲没有区别,Alexandrescu 推荐当期望的模板参数是一个 class struct 时使用 class 而在其他情况下使用 typename。 这样做的目的纯粹是为了增加程序的可读性。

[编辑] 消除依赖性名字的二义性

关键词 typename 也可以用来指明模板中的 dependent name 引用的是一个类型。

struct Foo {
  typedef int Type;
};
 
template<class T>
struct Bar {
  typedef typename T::Type BarType; // In a typedef
  void baz() {
     typename T::Type instance;     // In a declaration
  }
};
 
typedef Bar<Foo> FooBar;


相关主题: class, template

个人工具
名字空间
操作
导航
工具箱
其他语言