重新声明 class 名称 class-键
Redeclared class name class-key
以下引自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
我认为这意味着我们不能使用另一个 class 键声明显式特化,例如:
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();
};
但它工作正常。那么这条规则有什么意义呢?
在引用的句子之后是对 [dcl.type.elab] 的引用。 [dcl.type.elab]/p3 描述了 "agree in kind" 的意思:
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, the enum
keyword shall be
used to refer to an enumeration (7.2), the union
class-key shall
be used to refer to a union (Clause 9), and either the class
or
struct
class-key shall be used to refer to a class (Clause 9)
declared using the class
or struct
class-key.
换句话说,如果主模板是联合,"redeclaration, partial specialization, explicit specialization or explicit instantiation"必须使用union
;否则它可以使用 class
或 struct
,但不能使用 union
。
以下引自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
我认为这意味着我们不能使用另一个 class 键声明显式特化,例如:
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();
};
但它工作正常。那么这条规则有什么意义呢?
在引用的句子之后是对 [dcl.type.elab] 的引用。 [dcl.type.elab]/p3 描述了 "agree in kind" 的意思:
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.
换句话说,如果主模板是联合,"redeclaration, partial specialization, explicit specialization or explicit instantiation"必须使用union
;否则它可以使用 class
或 struct
,但不能使用 union
。