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 [...]
这篇关于为什么具有“相同签名"的模板和非模板函数的重载?调用非模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么具有“相同签名"的模板和非模板函数


基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01