- 普通函数调用时,可以发生自动类型转换
- 函数模板调用时,如果自动类型推导,不会发生隐式类型转换
- 函数模板调用时,如果利用显示指定类型的方式,可以发生隐式类型转换
#include <iostream>
using namespace std;
int addition1(int a, int b)
{
return a + b;
}
template<class T>
T addition2(T a, T b)
{
return a + b;
}
int main()
{
int a = 10;
char b = 'b'; // b - 98
cout << addition1(a, b) << endl;
// 编译器无法自动转换类型
// cout << addtion2(a, b) << endl;
// 模板函数需要显式指定类型,才能完成自动类型转换
cout << addition2<int>(a, b) << endl;
system("pause");
return 0;
}