c++ - 别名模板特化

标签 c++ templates c++11

别名模板 (14.5.7) 可以显式特化 (14.7.3) 吗?

我的标准功能失败了,我找不到要测试的编译器。

文本“当模板 ID 引用别名模板的特化时”暗示 ,但随后该示例似乎引用了其他东西,暗示 .

注意。我在 n3242 工作,它位于 FDIS 后面,本节的标题是“别名模板”。哈哈。

最佳答案

标准中“特化”的意思是将通用模板转换为更专业的实体。例如,实例化一个非成员类模板会产生一个不再是模板的类。术语“特化”有两个方面,可以指生成的特化(这是实例化的特化,可能来自部分特化)和显式特化(您所指的)。

别名模板没有被实例化,也没有它们的特化。他们没有什么可以实例化的。相反,只要它们的名称后跟模板参数列表,表示的类型就是您通过用别名类型替换名称和参数列表获得的类型,将所有模板参数引用替换为参数列表中给出的参数。也就是说,不是将其特化为别名,而是别名模板本身用作别名,而不需要实例化任何东西。这种替换很早就完成了。考虑:

template<typename T> using ref = T&;
template<typename T> void f(ref<T> x) { x = 10; }
int main() { int a; f(a); return a; /* 10 */ }

更换完成时间为ref<T>被命名(这样的名称用于引用类或函数模板特化;因此规范将此类名称描述为“引用别名模板的特化”)。即f的参数有类型 T& ,因此,T可以推断。此属性防止别名模板的显式或部分特化。因为为了选择ref的正确特化,它需要知道T .但是要知道它需要比较ref<T>根据参数类型推导出 T .总结在论文N1406, "Proposed addition to C++: Typedef Templates" ,第 2.2 节

2.2 The Main Choice: Specialization vs. Everything Else

After discussion on the reflectors and in the Evolution WG, it turns out that we have to choose between two mutually exclusive models:

  1. A typedef template is not itself an alias; only the (possibly-specialized) instantiations of the typedef template are aliases. This choice allows us to have specialization of typedef templates.

  2. A typedef template is itself an alias; it cannot be specialized. This choice would allow:

    • deduction on typedef template function parameters (see 2.4)
    • a declaration expressed using typedef templates be the same as the declaration without typedef templates (see 2.5)
    • typedef templates to match template template parameters (see 2.6)

需要注意的是,支持选项 1 的引用论文并未进入 C++0x。


编辑:因为您迫切希望有一个明确的规范引用。 14.5p3 是这样的

Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.

关于c++ - 别名模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622452/

相关文章:

c++ - 在 C++ 中编译时间映射 "search"

c++ - std::initializer_list 作为 std::array 构造函数

c++ - 使用 move 赋值运算符分配刚构造的未命名值

c++ - 如何延长局部变量的生命周期或使用引用的正确方法是什么

c++ - 如何在现代 C++ 中解析文本数据文件?

c++ - 使用 foreach 语法进行迭代时如何检查我是否在最后一个元素上

在 Visual Studio 2005 上使用 TCHAR 的 C++ 模板函数特化

templates - 具有特征和函数重载的静态类型语言?

c++ - 编译头文件定义了一个模板类,其中还包括其他头文件

c++ - 阿姆斯壮计算功能崩溃