【发布时间】:2014-07-27 16:46:13
【问题描述】:
模板模板类型名称?
当使用template <template <typename> class T>中的模板模板语法时,需要使用关键字class,因为使用typename会产生如下错误:
错误:模板模板参数在参数列表后需要'class'
在其他任何地方,关键字typename 和class 在声明模板参数的基本情况下是可以互换的。
您可能会争辩说,使用模板模板时的要求是暗示您应该传递一个类类型,但情况并非总是如此(尤其是在 C++11 引入模板化之后类型别名)。
template <template <typename> class T> // 'class' keyword required.
struct Foo {
using type = T<int>;
};
template <typename T>
using type = T (*)();
using func_ptr_t = Foo<type>::type;
这背后的原因是什么?
- 是否有任何具体原因说明为什么模板模板声明中不允许
typename? - C++ 标准对此有什么规定吗?
【问题讨论】:
-
现在有一个提议允许
typename。 -
我更惊讶的是它不允许结构。
-
GCC 5 现在允许在模板模板参数中使用 typename
标签: c++ templates c++11 language-lawyer typename