类模板的别名 [英] alias of class template
问题描述
在下面的代码中考虑一个类似于 A
的别名模板。现在让 B
为 A
的别名模板。
Consider an alias template like the A
in the code below. Now let B
be an alias template of A
.
在下面的代码中,这些类模板用作结构 C
的模板参数,该结构仅专用于一种类型名称( A
)。 clang -std = c ++ 11
存在,错误为:未定义模板'C< B>'
的隐式实例化指示
In the code below these class templates are used as template arguments for a struct C
which is only specialized for one typename (A
). clang -std=c++11
exists with error: implicit instantiation of undefined template 'C<B>'
indicating that another specialization for B
is needed.
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
被视为不同的类模板?是否有解决方法可以让我重命名冗长的模板而不会引起此问题?
Why (if even) is it that - despite not allowing specializations of aliases - A
and B
are treated as different class templates? Is there a workaround allowing me to rename a lengthy template without incurring this problem?
推荐答案
这是 CWG问题#1286 ,该示例涉及以下示例:
This is CWG issue #1286, which deals with this example:
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
具有相同的类型。
questioning whether or not y
and z
have the same type.
基本上,根据标准,c是正确的拒绝代码。 [temp.alias]告诉我们的是:
Basically, according to the Standard, clang is correct in rejecting the code. All [temp.alias] tells us is:
当 template-id 引用别名模板的特殊化,等效于通过将其 template-argument s替换为 template-parameter s而获得的关联类型
在别名
模板的 type-id 中。
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
>等价物。有一项拟议的决议将使它们如此,但尚未获得批准。
So while A<X>
is equivalent to B<X>
(for all X
!), there is no wording that A
is equivalent to B
. But on some level that doesn't really make any sense since B
and A
should be equivalent. There is a proposed resolution that would make them so, but it has not yet been approved.
这篇关于类模板的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!