Why does overload of template and non-template function with the quot;same signaturequot; 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) 不是更差的转换,则可行函数 F1 被定义为比另一个可行函数 F2 更好的函数序列比ICSi(F2),然后[...] F1是一个非模板函数,F2是一个函数模板特化 [...]
[...] 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 [...]
这篇关于为什么具有“相同签名"的模板和非模板函数的重载?调用非模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么具有“相同签名"的模板和非模板函数


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01