模板参数替换是否令人惊讶? [英] Surprise in template parameter substitution?

查看:72
本文介绍了模板参数替换是否令人惊讶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3690,第14.8.2条第3款有一个漂亮的例子:

N3690, § 14.8.2 paragraph 3 has this pretty mind blowing example:

template <class Z> void h(Z, Z*);
// #5: function type is h(int, const int*)
h<const int>(1,0);

问题:为什么不是h(const int, const int*)?

据了解,Z = const int,因此在模板声明中每次出现的Z都可以读为const int,还是我遗漏了什么?为什么指针不同?我记得当参数具有T&T*时,它会保留T的cv限定词,但我看不出有任何在此处应用它的可能性.

From what know, Z = const int, so every occurence of Z in the template declaration can be read as const int, or am I missing something? Why pointer is different? I remember that when parameter has T& or T* it preserves cv-qualifiers of T, but I don't see any possibility to apply it here.

推荐答案

您需要查看[dcl.fct]/5,原因如下:

You need to see [dcl.fct]/5 for the reason why:

单个名称可以在单个作用域中用于多个不同的功能;这是函数重载(第13条).函数的所有声明在返回类型和参数类型列表中都应完全一致.函数的类型使用以下规则确定.每个参数(包括函数参数包)的类型由其自己的decl-specifier-seq和声明符确定.在确定每个参数的类型之后,将类型为"T的数组"或返回T的函数"的任何参数分别调整为指向T的指针"或返回T的函数指针". 生成参数类型列表后,在形成函数类型时会删除所有修改参数类型的顶级简历限定符.结果转换后的参数类型列表以及是否存在省略号或功能参数包就是功能的parameter-type-list. [注意:此转换不会影响参数的类型.例如,int()(const int p,decltype(p))和int()(int,const int )是相同的类型. —尾注]

A single name can be used for several different functions in a single scope; this is function overloading (Clause 13). All declarations for a function shall agree exactly in both the return type and the parameter-type-list. The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively. After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. The resulting list of transformed parameter types and the presence or absence of the ellipsis or a function parameter pack is the function’s parameter-type-list. [ Note: This transformation does not affect the types of the parameters. For example, int()(const int p, decltype(p)) and int()(int, const int) are identical types. —end note ]

并且在const type* > const不是顶级const限定符.

这篇关于模板参数替换是否令人惊讶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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