别名模板专业化 [英] Alias template specialisation

查看:21
本文介绍了别名模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can alias templates (14.5.7) be explicitly specialised (14.7.3)?

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

My standard-fu fails me, and I can't find a compiler to test on.

文本when a template-id 指的是别名模板的特化"暗示,但是该示例似乎指的是其他东西,暗示.

The text "when a template-id refers to the specialization of an alias template" implies yes, but then the example appears to refer to something else, implying no.

注意.我在 n3242 上工作,它位于 FDIS 后面,其中本节的标题是别名模板".哈哈.

NB. I'm working from n3242, one behind the FDIS, in which the title of this section is "Aliase templates". Lol.

推荐答案

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

What the Standard means by "specialization" is the transformation of a generic template to a more specialized entity. For example, instantiating a non-member class template yields a class that's not a template anymore. The term "specialization" is two fold, and can refer to a generated specialization (which is a specialization that was instantiated, possibly from a partial specialization) and to an explicit specialization (which is what you referred to).

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

Alias templates aren't instantiated and there aren't specializations of them. There is nothing they could instantiate to. Instead, whenever their name is followed by a template argument list, the type denoted is the type you get by replacing the name and argument list by the alias'ed type, replacing all template parameter references with the arguments given in the argument list. That is, rather than the specialization of it being an alias, the alias template itself serves as an alias, without the need to instantiate anything. This replacement is done very early. Consider:

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 被命名时完成的(这样的名字被用来指代类或函数模板特化;因此规范描述了这样的名字指的是特化别名模板").即f的参数为T&类型,因此可以推导出T.此属性阻止了别名模板的显式或部分特化.因为为了选择正确的ref特化,需要知道T.但是要知道它,它需要将 ref 与参数类型进行比较以推导出 T.它总结在论文 N1406,建议添加到C++:Typedef 模板",第 2.2 节

The replacement is done at the time ref<T> is named (such a names are used to refer to class or function template specializations; hence the spec describes such names to "refer to the specialization of an alias template"). That is, the parameter of f has type T&, and thus, T can be deduced. This property is preventing explicit or partial specializations of alias templates. Because in order to pick the correct specialization of ref, it needs to know T. But to know it, it needs to compare ref<T> against the argument type to deduce T. It's summarized in the paper N1406, "Proposed addition to C++: Typedef Templates", section 2.2

2.2 主要选择:专业化与其他一切

在对反射器和 Evolution WG 进行讨论后,结果证明我们必须在两个相互排斥的模型之间进行选择:

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

  1. typedef 模板本身不是别名;只有 typedef 模板的(可能是专门的)实例是别名.这种选择使我们能够专门化 typedef 模板.

  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.

typedef 模板本身就是一个别名;它不能被专门化.此选择将允许:

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

  • 推导 typedef 模板函数参数(见 2.4)
  • 使用 typedef 模板表达的声明与不使用 typedef 模板的声明相同typedef 模板(见 2.5)
  • typedef 模板以匹配模板模板参数(参见 2.6)

需要注意的是,支持选项 1 的引用论文并未将其纳入 C++0x.

It should be noted that the quoted paper, which favors option 1, did not make it into C++0x.

因为您非常希望有一个规范引用如此明确.14.5p3 是这样的

Because you desperately want to have a spec quote saying so explicitly. 14.5p3 is one that does

因为别名声明不能声明模板 ID,所以不可能部分或显式地特化别名模板.

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

这篇关于别名模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆