简体   繁体   English

相同函数的模板化和非模板化版本是否被视为过载?

[英]Is a templated and a nontemplated version of the same function considered an overload?

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

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; 首先,根据标准(§13的开头):“当在同一范围内为单个名称指定两个或更多不同的声明时,该名称被称为重载。[...]仅功能和功能模板声明可以重载; variable and type declarations cannot be overloaded.” So clearly, your two declarations are overloads. 变量和类型声明不能重载。“很明显,你的两个声明都是重载。

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. 如果你调用myFunction( 3.14159 ) ,那么模板将使用与非模板相同的签名进行实例化,并且两者都是完全匹配。 In this case (§13.3.1): 在这种情况下(§13.3.1):

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, [...] 鉴于这些定义,可行函数F1被定义为比另一个可行函数F2更好的函数,如果对于所有参数i,ICSi(F1)不是比ICSi(F2)更差的转换序列,然后[...] - F1是非模板函数,F2是函数模板特化,[...]

The standard has specified your exact case. 该标准已指定您的确切案例。

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. 一般来说,结果是相同的,虽然http://www.gotw.ca/publications/mill17.htm指出一个异国情调(和写得不好?)的情况,他们不是。 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.) (有一个真正的例外:不提供通用实现有时很有用,但只有特殊化。根据我的经验,这种情况通常发生在traits类中,但也可能出现在单个函数中。在这种情况下,当然,你做专门的模板;否则你不能使用它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM