我刚刚发现GCC编译了这样的代码
template <typename A, typename B>
struct P {};
template <typename A>
template <typename B>
using Q = P<A, B>;
在哪里是一个双模板名称。
但我不能使用这个。当我写作时Q<short><long>
,我明白了
template_template.cpp:10:5: error: ‘Q<short int>’ is not a template
Q<short><long>{};
^~~~~~~~
template_template.cpp:10:20: error: invalid use of incomplete type ‘Q<short int>’
Q<short><long>{};
^
template_template.cpp:2:8: note: declaration of ‘Q<short int>’
struct P {};
为什么编译了第一个片段?
有没有一种语法来说服编译器Q<short>
实际上是一个模板?
发布于 2018-05-25 19:08:18
的声明在一个模板声明应 -声明或定义一个函数,类,或者一个变量,或 -定义成员函数,一个构件类,成员枚举,或类模板的或类的静态数据成员嵌套在类模板中,或者 - 定义类或类模板的成员模板,或 - 是别名声明
这里声明的内模板的声明是没有以上(这是另一种的模板声明,其本身包含一个别名声明),因此该代码是无效的。
语法的相关部分是:
template-declaration: template < template-parameter-list > 声明 别名声明: 使用标识符 attribute-specifier -seq opt = type-id ;
其中一个声明可以是模板的声明,一个别名声明,或其它类型的声明中。
请注意,语法本身接受给定的代码,但上面的文本中的其他限制使其无效。
https://stackoverflow.com/questions/-100004546
复制相似问题