#include <iostream>
using namespace std;
template <typename T>
T max(T a,T b)
{
return (a>b)?a:b;
}
int main()
{
cout<<"Max = "<<max(2,3)<<endl;
cout<<"Max = "<<max(2.1,3.1)<<endl;
cout<<"Max = "<<max(888888888,999999999)<<endl;
return 0;
}
这段代码 VC6 可以正常编译,但VS 2010 下提示如下错误:
1>TesT.cpp(13): error C2668: “max”: 对重载函数的调用不明确
1> TesT.cpp(6): 可能是“T max<int>(T,T)”
1> with
1> [
1> T=int
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xutility(2078): 或 “const _Ty &std::max<int>(const _Ty &,const _Ty &)”
1> with
1>