作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] ambiguity of function template overl …
時間Sat Dec 22 23:48:04 2007
※ 引述《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