Author Topic: Why would you rather use an explicit specialization in functions?  (Read 5118 times)

0 Members and 1 Guest are viewing this topic.

pllaybuoy

  • Guest
I was wondering why would you use explicit specialization for a template rather than using a function for specific type instead

consider the following functions
Code: (C) [Select]
//generic type
template <typename T>
void g_func(T & var$){
cout<<2+var$<<endl;
}

//explicit specialization for a user defined class object(salary)
template <> void g_func(salary & var$){
cout<<var$.amount+2<<endl;
}

int main(){

int a=500;
double b=600;
g_func(a);//creates function for int type from generic one
g_func(b);//creates function for double type from generic

salary c=800;//where salary is a user defined type

g_func(c);//uses template<> void g_func(salary & vaar$);


cin.get();
cin.get();
return 0;

}
So I wanted to know why is this method (explicit specialization) even there in the language ?
If you are going to define one for a specific type then why bother using the template <> sytanx? Why not just create a simple function like
Code: [Select]
void g_func(salary & var$); //simple function for user defined type
« Last Edit: February 04, 2013, 10:30:43 pm by skidiot.h »

Offline Teddy

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 8
    • View Profile
Re: Why would you rather use an explicit specialization in functions ?
« Reply #1 on: February 04, 2013, 01:10:51 pm »
Lets say you are creating a function like:
Code: [Select]
template <class T>
boolean compareTo(T& t1, T& t2) { return t1 == t2; } //I know the function has no sence
now you suddenly realise that the function does not really fit for the double values. There the values should be equal for a specific Arithmetic precision. For example:
compareTo(1.00000000000000000001, 1.0) would not be the same. But for your program they should be equal because the difference is to little.
You could fix this by using a explicit specialization.
Code: [Select]
template <> compareTo<double> (double& t1, double& t2) { double diff = t1 - t2; if(diff <0) diff *=-1; return diff  < epsilon} Now you still have the advantage of templates but you can use the function for double values as well. I know my example is silly but it was the best I could think of.
Anyways I think it is explained pretty well here (same story for class and function):
Code: [Select]
An explicit specialization of a class template provides an alternative definition of the primary template. It's used instead of the primary definition (or a partial specialization) if the arguments match those that are given in the explicit specialization.
When is an explicit specialization useful? Consider the Vectortemplate: The code generated by the compiler for the specializationVector<bool> is very inefficient. Instead of storing every Boolean value in a single bit, it occupies at least an entire byte. Following is an example of an explicit specialization Vector<bool> that manipulates bits rather than bytes.
Source: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=50
 

pllaybuoy

  • Guest
Re: Why would you rather use an explicit specialization in functions ?
« Reply #2 on: February 04, 2013, 04:37:26 pm »
Lets say you are creating a function like:
Code: [Select]
template <class T>
boolean compareTo(T& t1, T& t2) { return t1 == t2; } //I know the function has no sence
now you suddenly realise that the function does not really fit for the double values. There the values should be equal for a specific Arithmetic precision. For example:
compareTo(1.00000000000000000001, 1.0) would not be the same. But for your program they should be equal because the difference is to little.
You could fix this by using a explicit specialization.
Code: [Select]
template <> compareTo<double> (double& t1, double& t2) { double diff = t1 - t2; if(diff <0) diff *=-1; return diff  < epsilon} Now you still have the advantage of templates but you can use the function for double values as well. I know my example is silly but it was the best I could think of.
Anyways I think it is explained pretty well here (same story for class and function):
Code: [Select]
An explicit specialization of a class template provides an alternative definition of the primary template. It's used instead of the primary definition (or a partial specialization) if the arguments match those that are given in the explicit specialization.
When is an explicit specialization useful? Consider the Vectortemplate: The code generated by the compiler for the specializationVector<bool> is very inefficient. Instead of storing every Boolean value in a single bit, it occupies at least an entire byte. Following is an example of an explicit specialization Vector<bool> that manipulates bits rather than bytes.
Source: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=50

Hey thanks but I know it is used to define for a particular type that won't fit with the general type but my question is that why wont you just use a simple function for that particular type?

Staff Edit
Still can't use punctuation properly
« Last Edit: February 04, 2013, 10:24:08 pm by skidiot.h »

Offline Teddy

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 8
    • View Profile
Re: Why would you rather use an explicit specialization in functions ?
« Reply #3 on: February 04, 2013, 05:50:14 pm »
I think that question is not easy to answer. The effect is in both situations the same just the way it happans is different. In the one case function overloading is used and in the other case template specification.
 
But my question is. Would not it be a bad design to use a template for a function and than suddenly switch for a different type to function overloading. I mean let's assume you are using a third part library which defines the function isEqual() from the above example. Then you would use the whole time the library like this:
isEqual<int>(2,3);
isEqual<char>('a','b');
isEqual<double>(2.001,2.0);
If you would have used  function overloading for double values the function which you have written to handle double values in a special would not be called but the template function:
Code: [Select]
template <class T>
boolean compareTo(T t1, T t2) { return t1 == t2; }

But now you run into the same problem like before. You wanted the double values to be equal when the Arithmetic precision is good enough. So you would write in the specification of your library now:
Quote
template <class T> boolean compareTo(...) : Use the template if you want to use any non double or   float value. Otherwise see definition of: boolean compareTo(double a, double b);
In my opinion that would be a very bad design and would make your library less user friendly. So just use function overloading or templates. But do not mix these things up.

I mean you could let the whole time the compiler decide which function to pick. Then there would not be a difference again because function overloading is executed before the template functions. But still I would have a strange feeling because you would force the user to let the compiler pick the function and once someone uses the template like I above there woule be trouble again.
 
So to sum this up. I personally think it would be a bad design to mix up function overloading & templates. But may there is another person in the forum who can give me an example when/why it would be a good idea
« Last Edit: February 04, 2013, 06:00:30 pm by Teddy »

pllaybuoy

  • Guest
Re: Why would you rather use an explicit specialization in functions ?
« Reply #4 on: February 04, 2013, 06:59:19 pm »
Maybe you are right we should not mix things up, the sequence goes like this
inlinefunction~Function~template explicitly specialized function/or explicit instantiation if you've done it ~template function (generic form, compiler picks up the appropriate type).
Still this is a little confusing, maybe I'll get it by experience someday

Staff Edit
Still can't use a forum properly
« Last Edit: February 04, 2013, 10:26:50 pm by skidiot.h »

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Why would you rather use an explicit specialization in functions?
« Reply #5 on: February 05, 2013, 10:19:22 am »
@pllaybuoy; it's used to facilitate polymorphism through overloading.  Go look up the term, I'll be here to explain it to you later.
-Xires

pllaybuoy

  • Guest
Re: Why would you rather use an explicit specialization in functions?
« Reply #6 on: February 05, 2013, 11:29:11 am »
@pllaybuoy; it's used to facilitate polymorphism through overloading.  Go look up the term, I'll be here to explain it to you later.
Yah , I know a little about polymorphism . Function overloading , operator overloading , function templates and class templates (I guess this is polymorphism).