重载template失败,原因?
ao168 2005-11-25 11:20:19 //非模板函数重载
const char* min(const char* ch1, const char* ch2,const char* ch3)
{
const char* temp;
int result1 = strcmp(ch1,ch2);
int result2 = strcmp(ch1,ch3);
int result3 = strcmp(ch2,ch1);
int result4 = strcmp(ch2,ch3);
if((result1<0)&&(result2<0)) { temp = ch1; }
else if((result3<0)&&(result4<0)) { temp=ch2; }
else { temp=ch3; }
cout<<"r1 = "<<result1<<",r2 = "<<result2<<",r3 = "<<result3<<", r4 = "<<result4<<endl;
return temp;
}
template <class T>
T min(T ii, T jj, T kk)
{
T temp;
if((ii<jj)&&(ii<kk)){ temp=ii; }
else if((jj<ii)&&(jj<kk)){ temp=jj; }
else { temp=kk; }
return temp;
}
int main(int argc, char* argv[])
{
cout<<min(100,20,30)<<endl;
cout<<min("Good Anderson","Washington","aGoogle Smith")<<endl;
return 0;
}
//结果应该返回: aGoogle Smith ,却返回了: Good Anderson