c++ - 别名模板特化

标签 c++ templates c++11

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

我的 standard-fu 失败了,我找不到可以测试的编译器。

文本“当模板 ID 指的是别名模板的特化时”暗示,但随后该示例似乎指的是其他内容,暗示 .

NB. 我在 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/27766798/

相关文章:

c++ - (GTK :4783): Gtk-CRITICAL **: gtk_image_set_from_pixbuf: assertion 'GTK_IS_IMAGE (image)' failed

c++ - 为什么 std::put_time(std::gmtime with the %z format give back +0100 for UTC?

c++ - 如何包装一个类然后调用它的方法?

c++ - 在预处理器指令中使用变量

c++ - OpenGL - 告诉顶点着色器 VBO 已经改变

c++ - 具有函数模板的递归函数

c++ - 我可以告诉 C++ 编译器如何自己进行任意类型转换吗?

c++ - 局部变量的引用崩溃

c++ - VS2013 中的类型特征

c++ - 如果已经被互斥锁保护,我是否需要使用 atomic<>