是模板化的还是非模板化的同一个函数的版本被认为是超载? [英] Is a templated and a nontemplated version of the same function considered an overload?

查看:181
本文介绍了是模板化的还是非模板化的同一个函数的版本被认为是超载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常正式的问题:这是否被认为是过载?是否删除模板与仅对参数进行重载基本上不同?

A very formal question: is this considered an overload? Is removing the template fundamentally different than only overloading on arguments?

template<class T> void myFunction(const T& t) {}
void myFunction(const double& t) {}

那么后续的问题是,最好遵循这种方法还是使用模板专业化,而不是过载?

Then the follow up question, is it better to follow this approach or to use template specialization, instead of the overload?

template<> void myFunction(const double& t) {}


推荐答案

根据标准(§13开头):当在
同一作用域中为单个名称指定两个
或更多不同的声明时,该名称被称为重载。 ...]只有函数和
函数模板声明可以重载;变量和类型
声明不能重载。很明显,你的两个
声明是重载的。

First of all, according to the standard (start of §13): “When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded.[...]Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.” So clearly, your two declarations are overloads.

如果调用 myFunction(3.14159)模板将
实例化为与非模板相同的签名,并且两者将
是完全匹配。在这种情况下(§13.3.1):

If you call myFunction( 3.14159 ), then the template will be instantiated with the same signature as the non-template, and both will be exact matches. In this case (§13.3.1):


给定这些定义,可行函数F1定义为
函数比另一个可行函数F2如果对于所有参数
i,ICSi(F1)不是比ICSi(F2)更糟的转换序列,
然后
[...]
- F1是非模板函数,F2是函数模板
specialization,[...]

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then [...] — F1 is a non-template function and F2 is a function template specialization, [...]

对于特殊化函数的替代方案:
specializations可能是根据上面的定义的重载,但是
它们不参与重载分辨。专业化工作
不同:重载分辨率首先发生没有他们;那么,如果
重载分辨率已经选择了模板,并且对于实例化类型有一个
专门化,则专用化是
,而不是模板的通用实例化。一般来说,
的话,结果是一样的,虽然
http:// www .gotw.ca / publications / mill17.htm 指出一个异乎寻常的(和
不好写?)情况,他们不是。不过,至少对我来说,似乎
更自然地提供了重载函数,而不是
模板专门化。大多数时候,反正。 (有一个真正的
异常:有时有用的是不提供一个通用的
实现,但只有专门化。在我的经​​验中,这种
情况通常发生在traits类,但它可能发生在这种情况下,当然,你做的是
专门化模板;否则你不能使用它。)

With regards to the alternative of specializing the function: specializations may be overloads according to the definition above, but they do not participate in overload resolution. Specializations work differently: overload resolution first occurs without them; then, if overload resolution has chosen the template, and there is a specialization for the instantiation type(s), the specialization is used, rather than the generic instantiation of the template. Generally speaking, the results are the same, although http://www.gotw.ca/publications/mill17.htm points out one exotic (and badly written?) case where they aren't. Still, to me at least, it seems more natural to provide the overloaded function, rather than the template specialization. Most of the time, anyway. (There is one real exception: it's sometimes useful to not provide a generic implementation, but only specializations. In my experience, this situation usually occurs with traits classes, but it can occur for an individual function as well. In such cases, of course, you do specialize the template; you cannot use it otherwise.)

这篇关于是模板化的还是非模板化的同一个函数的版本被认为是超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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