以下引用来自 14.5.1/4 [temp.class]
:
In a redeclaration, partial specialization, explicit specialization or explicit instantiation of a class template, the class-key shall agree in kind with the original class template declaration
我认为这意味着我们不能使用另一个类键声明显式特化,例如:
template <class T, class W>
struct A
{
void foo();
};
template <class T, class W>
class A<T*, W> // Should have printed an error
{
void foo();
};
<强> DEMO
但是效果很好。那么这条规则有什么意义呢?
最佳答案
在引用的句子之后是对 [dcl.type.elab] 的引用。 [dcl.type.elab]/p3 描述了“同意”的含义:
The class-key or
enum
keyword present in the elaborated-type-specifier shall agree in kind with the declaration to which the name in the elaborated-type-specifier refers. [...] Thus, in any elaborated-type-specifier, theenum
keyword shall be used to refer to an enumeration (7.2), theunion
class-key shall be used to refer to a union (Clause 9), and either theclass
orstruct
class-key shall be used to refer to a class (Clause 9) declared using theclass
orstruct
class-key.
换句话说,如果主模板是 union ,那么“重新声明、部分特化、显式特化或显式实例化”必须使用union
;否则它可以使用class
或struct
,但不能使用union
。
关于c++ - 重新声明类名 class-key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956250/