gpt4 book ai didi

templates - 通过模板别名显式实例化类

转载 作者:行者123 更新时间:2023-12-04 01:02:24 26 4
gpt4 key购买 nike

是否可以通过模板别名显式实例化模板类?

如果是这样,如何?否则,有人可以指出讨论并决定反对的 ISO 文件吗?

template<class T>
struct A { };

/// Explicit instantiate A for int:
template struct A<int>;

/// Alias
template<class T>
using B = A<T>;

/// Explicitly instantiate A for double via alias B:
template struct B<double>;
/// error: elaborated type refers to a non-tag type

这不应该实例化 A<double>B<T>只是 A<T> 的不同名称?

最佳答案

这是间接禁止的,因为:

7/3 禁止在没有类键的情况下编写显式特化( classstructunion ):

In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaborated-type-specifier with a class-key (9.1), or an enum-specifier.



7.1.6.3/2 禁止将类键与别名模板特化结合:

3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. ... If the identifier resolves to a typedef-name or the simple-template-id resolves to an alias template specialization, the elaborated-type-specifier is ill-formed.

关于templates - 通过模板别名显式实例化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118191/

26 4 0