c++ - 重新声明类名 class-key

标签 c++ templates

以下引用来自 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, 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.

换句话说,如果主模板是 union ,那么“重新声明、部分特化、显式特化或显式实例化”必须使用union;否则它可以使用classstruct,但不能使用union

关于c++ - 重新声明类名 class-key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956250/

相关文章:

c++ - string::size_type 真的大到可以容纳任何字符串吗?

c++ - 负数组索引和看似冗余的模板使用

c++ - 如何检查在编译时是否调用了模板化方法?

C++模板化接口(interface)

c++ - 我可以在 C++ 中从另一个构造函数调用构造函数(进行构造函数链接)吗?

c++ - 函数指针是否不支持 C++ 中的实例类型

c++ - boost 函数映射到字符串

c++ - 错误 C2678 : binary '!=' : no operator found which takes a left-hand operand

templates - 与其他模板引擎(如 Velocity)类似的模板习惯用法?

c++ - 在语法文件上运行 c++ 的 antlr4 解析器显示错误 33 : missing code generation template NonLocalAttrRefHeader