【发布时间】:2013-07-23 13:47:36
【问题描述】:
以下代码打印“First”。为什么选择第一个模板,而第二个似乎更专业,应该更匹配? (我用的是 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);
}
【问题讨论】:
标签: c++ templates generic-programming