功能专业化

时间:2014-04-07 22:21:19

标签: c++ templates

我试图弄清楚这与功能有什么区别。

第一个是用于添加到表达式的模板函数:

template <class T,class Y,class Z>
Z add(T t,Y y)
{
  return t+y;
}

Specialization_1:

template<>
int add<int,int,int>(int t,int y)
{

    return t+y+10000;
}

Specialization_2:

int add(int t,int y)
{

    return t+y+10000;
}

speciaization_1和specialization_2 之间的有什么区别? 是否有必要使用模板&lt;&gt;在宣布之前????

2 个答案:

答案 0 :(得分:2)

第一个是专业化。第二个是超载。

第一个将创建模板的特殊变量。第二个将创建另一个具有相同名称的函数

答案 1 :(得分:0)

我对你的第一次专业化没有兴趣。这更有用,例如:

template <typename T>
T add(T t,T y)
{
  return t+y+10000;
}

现在您可以使用此功能添加许多不同的类型。