#if 0
template<typename T>//模板头
T mau(T a, T b)
{
return a > b ? a : b;
}
template<typename T>
void foo(T const& a,T const& b)
{
cout << typeid(a).name() << " " << typeid(b).name()<<"\n";
}
int main()
{
int num1 = 12, num2 = 34;
cout << mau(num1,num2) << endl;
cout << mau(1.2, 3.4) << endl;
cout << mau("hello","world")<< endl;//推断出const char*
cout << mau<string>("hello", "world") << endl;
foo(num1, num2);
char c[32], d[32];
foo(c, d);
char e[32], f[128];
//foo(e,f);//const char[32] const char[128]有两个类型只有一个类型来接
//foo(1, 1.3); int double 隐式推断的同时不允许隐式转换 编译器报错
return 0;
}
#endif // 模板的隐式推断
模板的隐式推断
最新推荐文章于 2024-08-15 05:02:54 发布