为什么模板和非模板函数的重载具有“相同签名”调用非模板函数? [英] Why does overload of template and non-template function with the "same signature" call the non-template function?
本文介绍了为什么模板和非模板函数的重载具有“相同签名”调用非模板函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
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.
从C ++ 11标准的第13.3.3 / 1节:
From Paragraph 13.3.3/1 of the C++ 11 Standard:
给定这些定义,如果对于所有参数i,ICSi(F1)不是比ICSi(F1)更差的转换序列,则可行函数F1被定义为比另一个可行函数F2更好的函数 (...)
这篇关于为什么模板和非模板函数的重载具有“相同签名”调用非模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文