考虑像 A
这样的别名模板在下面的代码中。现在让 B
是 A
的别名模板.
在下面的代码中,这些类模板用作结构 C
的模板参数。仅专门用于一种类型名称( A
)。 clang -std=c++11
与 error: implicit instantiation of undefined template 'C<B>'
一起存在表示 B
的另一个特化是需要的。
template<int N>
using A = int;
template<int N>
using B = A<N>;
template<template<int> class I>
struct C;
template<>
struct C<A> {};
int main() {
C<A> c;
C<B> d; // clang error: implicit instantiation
}
为什么(如果甚至)是 - 尽管不允许别名的特化 - A
和 B
被视为不同的类模板?是否有解决方法允许我重命名冗长的模板而不会出现此问题?
最佳答案
这是 CWG issue #1286 ,处理这个例子:
template<template<class> class TT> struct X { }; template<class> struct Y { }; template<class T> using Z = Y<T>; X<Y> y; X<Z> z;
质疑是否y
和 z
具有相同的类型。
基本上,根据标准,clang 拒绝代码是正确的。所有 [temp.alias] 告诉我们的是:
When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template.
所以 A<X>
相当于B<X>
(对于所有 X
!),没有措辞 A
相当于B
.但在某种程度上,自 B
以来并没有任何意义。和 A
应该是等价的。有一项拟议的决议将使他们这样做,但尚未获得批准。
关于c++ - 类模板的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723988/