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'。根據你的解釋,我們在這裏不明確。但我們不。 –