0

I try to figure out what is difference between this to function.

First one is template function for adding to expression :

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;
}

What difference is between speciaization_1 and specialization_2 ? Is it necessary to use template<> before declaration????

Radek
  • 329
  • 2
  • 17
  • Template can be used with anyType while your function works only with `int` – Coldsteel48 Apr 07 '14 at 22:23
  • I know why we have to use template but I asked about difference betweeen spec_1 and spec_2 – Radek Apr 07 '14 at 22:30
  • possible duplicate of [Differences between template specialization and overloading for functions?](http://stackoverflow.com/questions/1511935/differences-between-template-specialization-and-overloading-for-functions) – Constructor Apr 07 '14 at 22:34

2 Answers2

2

the first is Specialization. the second is overloading.

the first will create a special varient of the template. and the second will create another function with the same name

asaf app
  • 384
  • 2
  • 3
  • 12
0

I don't see interest in your first specialization. this is more useful for example:

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

Now you can use this function add for many differents type.

Julien Leray
  • 1,647
  • 2
  • 18
  • 34