Class Template Specialization in C++

Written by Raza on. Posted in C++

We have been discussing templates in detail for over two weeks. First we explored function templates with a single parameter, then with multiple parameters and also learnt how to specialize template functions.  Currently we are exploring class templates. Today we shall see how we can specialize class templates for a particular data type. They are specialized like function templates and behave identically as well but they are declared with a different syntax. 

We worked with a class Fraction last time and constructed a function to add two fractions together. We shall carry on with the same class here as well. We want to specialize our function for float type this time. By doing this, we would be able to build a class template function capable of holding only that particular type of data.

To specialize a function, we construct another function with the same prototype for our special data type. Above that function, we write the keyword template followed by just two closed angular brackets. As we have learnt that the name of our template class gets changed, we shall write the name of the class for that particular data type in that function. So we online casino write a class template function with its specialized form as well.


<p>template &lt;typename T&gt;<br />
Fraction&lt;T&gt; Fraction&lt;T&gt; :: Add (const Fraction&lt;T&gt; &amp;obj)<br />
{<br />
.    Fraction&lt;T&gt; temp ;</p>
<p>.   temp.num = (num * obj.den) (den * obj.num) ;<br />
.   temp.den = den * obj.den ;<br />
.   return temp ;<br />
<p>template&lt;&gt;<br />
Fraction&lt;float&gt; Fraction&lt;float&gt; :: Add (const Fraction&lt;float&gt; &amp;obj)<br />
{<br />
.    Fraction&lt;float&gt; Hair <a href="">home drug test kits</a> or hair follicle <a href="">home drug test kits</a> may reveal practically all types of drug toxins: - Opiates from synthetic analogs (heroin, morphine, codeine, methadone etc. temp ;<br />
.    temp.num = (num * obj.den) (den * obj.num) ;<br />
.    temp.den = den * obj.den ;<br />
.    return temp ;<br />


When we declare our object for float type and call the add function, the specialized function would now be called. We say that the specialized function over rides the default template function. We can build specialized functions for as many data types as we like. Note that generic programming is very vast topic and I have only just touched upon that. There are many ways to make template classes and many ways to specialize a template class for a certain data type. We can build an entire class for one specific data type. In classes, template specialization is of two types; partial and complete. In short, I have not explained all these scenarios because we might get lost in the depths of this topic. For all practical purposes, this explanation is quite sufficient. Maybe some other time I would explain templates and its specialization technique. But from next time, I would be going again into the object oriented paradigm.

Tags: , , , , ,