Function Template Specialization in C++

Written by Raza on. Posted in C++

Templates are a very powerful tool in programming. They constitute the so called feature of Type Independent or Generic Programming. We have seen that using templates with multiple parameters or a single parameter, we can save our time and energy and write a code in a generic manner instead of writing it for each and every data type. However, as a thing becomes powerful, there are also certain potential dangers associated with it. One such scenario arises with templates quite frequently while its solution lies in template specialization. This type of specialization s known as Complete Specialization.

The problem is that our template functions work pretty well for most of the built in data types as int, char, float, double, etc. however, it is not necessary that our template function would work accurately for each and every data type that exists in the language. Remarkably though, there may be disastrous results for us if we do not take this thing into account. Have a look at the following code.

 

</p>
<p>template &lt;typename T&gt;</p>
<p>T compare (T a, T b)</p>
<p>{</p>
<p>if (a &lt; b)</p>
<p>return a ;</p>
<p>else</p>
<p>return b ;</p>
<p>}</p>
<p>&nbsp;</p>
<p>int main ()</p>
<p>{</p>
<p>int a = 2 ;</p>
<p>int b = 4 ;</p>
<p>int *aptr = &amp;a ;</p>
<p>int *bptr = &amp;b ;</p>
<p>cout &lt;&lt; compare (aptr, bptr) ;</p>
<p>}</p>
<p>

 

Carefully observe what is going on here. aptr and bptr are two pointers to integers. They are passed as parameters to template function. Then comes the wrong part! We compare those two pointers and return the one which is lesser. The mistake we are doing unknowingly is that we are comparing the addresses of the two pointers. Actually we need to compare the values of the integers these pointers are pointing to. For that we need to dereference these pointers which we are not doing. So our template function is not capable of handling pointers. We need to find a way so that if a pointer is passed to it, it should handle it pretty well. In fact, we need our template function to be specialized for integer online casino canada pointers.

Now there is no end to the data types. Apart from pointers, there are user defined data types of enum, struct, class and namespace type as well. Then, there are many types of pointers. Without going into the details of user defined data types for now, we try to handle this situation when an integer pointer is passed to our template function. As this is a special scenario, we need to specialize our template function. What we generally do is that we write the same template function again with our special data types as parameters. Writing this explicit function is known as template specialization.  First look at the code below.

 

</p>
<p>template &lt;typename T&gt;</p>
<p>T compare (T a, T b)</p>
<p>{</p>
<p>if (a &lt; b)</p>
<p>return a ;</p>
<p>else</p>
<p>return b ;</p>
<p>}</p>
<p>&nbsp;</p>
<p>int compare (int *a, int *b)</p>
<p>{</p>
<p>if (*a &lt; *b)</p>
<p>return *a ;</p>
<p>else</p>
<p>return *b ;</p>
<p>}</p>
<p>&nbsp;</p>
<p>int main ()</p>
<p>{</p>
<p>int a = 2 ;</p>
<p>int b = 4 ;</p>
<p>int *aptr = &amp;a ;</p>
<p>int *bptr = &amp;b ;</p>
<p>cout &lt;&lt; compare (aptr, bptr) ;</p>
<p>}</p>
<p>

 

The trick in template specialization is to redefine that function in exactly the same way as it has been done previously with the template. The name of the function and number of parameters should match exactly. When compare function is called, compiler goes to the template function first. It looks at the data type sent to it and then goes through the rest of the program to see if any function has been specialized for that particular data type? If it finds one such function, it executes that function instead of the template function.

This way, we reduce the possibility of any dangerous condition in our program. There is one drawback of template specialization as well. If we are using a character pointer in our program, we would have to specialize this function for char pointer separately. Similarly for each data type, it would have to be redefined which reduces the efficiency of generic programming. For now, it seems fine and nice. We will gradually learn those techniques with which we would be able to solve this problem as well.

Tags: , , ,