→ legnaleurc:array 不是 pointer 03/09 22:19
※ 編輯: ro1234 來自: 218.161.52.139 (03/09 23:57)
在function template與一般function的選擇中
小弟最近弄得有點模糊
所以來請教各位
template<class T>
int test(const T&,const T&)
{
cout<<"Class"<<endl;
}
int test(const char*,const char*)
{
cout<<"Function"<<endl;
}
int main()
{
char x[]="Apple";
char y[]="abcd";
char* x1=x;
char* x2=y;
test(x,y);
system("pause");
return 0;
}
在上述中,
如果以x與y喚起test則會使用一般函式
若以x1與x2則會喚起template版本
使用x與y時,template參數會推導成char而不是推導成char*
因此會喚起一般的函式。
在這裡為什麼它不是將template參數推導成char*呢?
因為在c++ primer4/e中,他說參數會推導成 char*,但是我試驗之後卻沒有
(因為我將一般函式刪除後出現的錯誤是找不到
test(const char[6],const char[5]))
而如果以x1和x2傳入,則template參數會推導成char*,
因此喚起template版本
這又是為什麼不喚起一般版本,兩個函式的參數不都是const char*
這兩種呼叫方式的差別在何處?
講得很模糊,不知道各位有沒有看懂?
謝謝各位的解答<(_ _)>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.161.52.139