在模板里, 你可以抽象地把typename理解为一种数据类型(它应该是算成静态的数据类型)
你在template中定义的是typename 1和2, 没有T, 所以fun(T a , T b)就肯定是错的, 因为你定的是1和2, 就没有T的事.
这个和重载差不多, 只是说重载是对于不同参数进行不同的操作, 这个是对于不同的数据类型, 进行相同的操作, 但是这样, 模板也是可以减少许多不必要的代码书写.
正确写法是:
template
_1 fun (_1 a, _2 b)
{
......
return a;
}
你在哪里看到的, 原样抄过来, 别按自己理解写一个.
template
你这里的是错的
T是在你调用的时候来确定的,给你个例子:
#include
using namespace std;
template
T fun(int a, int b)
{
b = a - b;
return b;
}
int main()
{
int result = fun
cout << result << endl;
return 0;
}