【发布时间】:2017-02-16 01:07:01
【问题描述】:
假设我有两个函数的重载
template <typename T>
void f(const T&) {
cout << "f(T&)" << endl;
}
template <typename T>
void f(const T*) {
cout << "f(T*)" << endl;
}
为什么f(new int) 解析为f(const T&) 而不是f(const T*)?标准中的任何地方都谈到了这种违反直觉的行为?
【问题讨论】:
-
f(new int)解析为f(const T&),T设置为int*,而不是T设置为int。就好像您正在调用带有签名的函数。f(int* const&); -
我知道这部分。我的问题是,为什么
f(int* const&)比f(const int*)更匹配?
标签: c++ c++11 overloading