看板 EE_DSnP 關於我們 聯絡資訊
※ 引述《popo4231 (小泰)》之銘言: : code: : #include<iostream> : using namespace std; : template<class T> : T x(T i) : { : return i+1; : } : int x(int i) : { : return i; : } : int main() : { : double d = 1.56; : char c = 'a'; : int i = 10; : cout<<x(i)<<"\n"<<x(c)<<"\n"<<x(d)<<endl; : system("pause"); : return 0; : } : result: : 10 : b : 2.56 : 請按任意鍵繼續 . . . : 可是竟然沒有compile error! : 我覺得那兩個x函式會產生ambiguity阿 : 可是好像compiler會先挑非template的執行 : why? non-template funciton 與 template function 的 function symbol 本來就不太一樣, 所以 compiler 不會把他們搞錯, 而且會先找 non-template 版本的. 至於為什麼... 定義如此吧? 如果一定要呼叫 template one, 請 explicitly specify: x<int>(10) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.121.133.136