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

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

问题描述

别名模板(14.5.7)是否可以被显式地专门化(14.7.3)?



我的标准库不能使用,



文本当模板标识引用别名模板的特殊化意味着,但是示例

,一个在FDIS后面,其中本节的标题是别名模板。 Lol。

解决方案

特殊化的标准意味着通用模板到 >更专业的实体。例如,实例化非成员类模板会生成不再是模板的类。术语特殊化是两个方面,并且可以指生成的特化(其是实例化的特殊化,可能来自部分特化)和显式特化(这是您所指的)。



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

 模板< typename T>使用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& ,因此, code>可以推导出来。此属性阻止别名模板的显式或部分专门化。因为为了选择 ref 的正确的专业化,它需要知道 T 。但是要知道它,它需要比较 ref< T> 与参数类型来推导 T 。本文摘要介绍了 N1406,建议添加到C ++:Typedef模板 ,第2.2节



$ b


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



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


  1. typedef模板本身不是别名;只有typedef模板的(可能专用的)实例化是别名。


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




    • 对typedef模板函数参数进行扣除(见2.4)

    • 使用typedef模板表示与没有
      typedef模板的声明相同(见2.5)

    • typedef模板匹配模板模板参数(见2.6)



需要注意的是,没有使它成为C ++ 0x。






编辑:因为你迫切地想要一个规范引用。 14.5p3是一个


因为别名声明不能声明模板id,所以不可能部分或明确地专门化别名模板。



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.

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.

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 */ }

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 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)

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


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

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

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

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