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????

3

2 Answers 2

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

2
  • SO when is better to use specialization over overloading ?
    – Radek
    Commented Apr 7, 2014 at 22:32
  • this may helphttp://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading
    – asaf app
    Commented Apr 7, 2014 at 22:36
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.