类模板的别名

Lmi*_*mis 5 c++ templates typedef c++11

考虑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
}
Run Code Online (Sandbox Code Playgroud)

为什么(如果甚至) - 尽管不允许别名的特化 -A并且B被视为不同的类模板?是否有解决方法允许我重命名冗长的模板而不会出现此问题?

Bar*_*rry 4

这是CWG 问题 #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;
Run Code Online (Sandbox Code Playgroud)

询问 和 是否y具有z相同类型。

基本上,根据标准,clang 拒绝代码是正确的。[temp.alias] 告诉我们的是:

template-id引用别名 template 的特化时,它相当于通过用其template-argument替换别名模板的type-id中的template-parameter获得的关联类型。

因此, whileA<X>相当于B<X>(对于所有人X!),但没有任何措辞A相当于B。但在某种程度上,这并没有任何意义,因为B并且A 应该是等效的。有一项拟议的决议可以使他们这样做,但尚未获得批准。