4
以下代码打印“第一个”。为什么选择第一个模板,而第二个模板似乎更专业化,应该是更好的匹配? (我使用MSVC10)重载模板分辨率
我知道这与第二个模板通过const &
接受其参数的事实有关,但仍不能理解为什么这使得它更糟糕的匹配。
#include <map>
#include <iostream>
template<class Range>
void go(Range &r)
{
std::cout << "First" << std::endl;
}
template<class K, class V>
void go(const std::map<K, V> &m)
{
std::cout << "Second" << std::endl;
}
int main()
{
std::map<int, int> m;
go(m);
}
我希望没关系,我已经用测试编辑了答案。 – user1810087
@itwasntpete固定标点符号,谢谢。 –
那么,实例化在重载决议之前?让我们从第二个例子中删除'const'。根据你的解释,我们在这里不明确。但我们不。 –