c++ - 类模板的别名

标签 c++ templates c++11 typedef

考虑像 A 这样的别名模板在下面的代码中。现在让 BA 的别名模板.

在下面的代码中,这些类模板用作结构 C 的模板参数。仅专门用于一种类型名称( A )。 clang -std=c++11error: 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
}

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

最佳答案

这是 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;

质疑是否yz具有相同的类型。

基本上,根据标准,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/

相关文章:

c++11 - move 容器的迭代器?

c++ - 使 C++ 初始化程序自动检测 union 成员?

c++ - C 和 C++ 源代码分析工具

c++ - 函数模板中的 lambda 闭包类型和默认参数

c++ - 如何确定输出流链是否结束?

templates - 多个文件的划分模板不提供数据

c++ - 一个类的双重部分模板特化

c++ - 如何从一个进程向多个其他进程发出信号?

c++ - 无法将...从 '<brace-enclosed initializer list>' 转换为 map

c++ - 打印一个已知大小的结构