1
Class Wow{
public:
//main metod
template<typename T>
void foo(T t){
cout << t << endl;
}
template<>
void foo<int>(int t){
cout << "specialization" << endl;
}
void foo(int t){
cout << "overloading" << endl;
}
}
和主要是
Wow wow;
wow.foo(2.2);
wow.foo(1);
這個輸出
2.2
overloading
我的問題是爲什麼,即使編譯?實際上,,foo
被定義爲void foo(int)
兩次。
1)爲什麼通過?
2)爲什麼編譯器選擇重載?
謝謝
重載總是被視爲比模板實例更好的匹配。即使模板實例是明確的專業化。如果我有一個標準的副本,我會引用它。 – StoryTeller
答案2)。但爲什麼它讓歧義通過? – Gulzar
因爲如果一個更好的匹配,沒有歧義。模糊性是編譯器沒有可以決定調用其中一個的規則。 – StoryTeller