简体   繁体   English

为什么带有“相同签名”的模板和非模板函数的重载调用非模板函数?

[英]Why does overload of template and non-template function with the “same signature” call the non-template function?

I have this code: 我有这个代码:

template<
    class T = const int &
> void f(T) {}

void f(const int &) {}

int main() {
   f(0);
}

Why does it call the second one instead of first? 为什么称它为第二个而不是第一个? I would think of them as being the same but they're clearly not as I do not get a redefinition error. 我会认为它们是相同的,但它们显然不是因为我没有得到重新定义错误。

Because the second overload is not a template. 因为第二个重载不是模板。

When a template function and a non-template function are both viable for resolving a function call, the non-template function is selected. 当模板函数和非模板函数都可用于解析函数调用时,选择非模板函数。

From Paragraph 13.3.3/1 of the C++ 11 Standard: 从C ++ 11标准的第13.3.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是函数模板专业化 [...]

One is a template and the other is not, they are definitely not the same. 一个是模板而另一个不是,它们肯定不一样。

Overload resolution is designed to prefer a non-template over a templated function, everything else being equal. 过载分辨率设计为优先于模板化函数的非模板,其他一切都相同。

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

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