C++ Class Template Specialization

After all the knowledge about generic programming using C++ class template, the next one to know will be about some sort of specifics called C++ Class Template Specialization. There is an exception to every rule. Any generic code development will need a small case where it needs to do some hard coding or to avoid some amount generic code. That is where the C++ class Template Specialization comes in.

The idea of C++ class template specialization is similar to function template overloading. This can make the template code for certain data types to be fixed. For example if one wants to write a Queue class template with the ability to handle all data types. If he has a good ready made Queue class for character arrays, he would definitely be interested in re-using it.

The above requirement can be made possible by C++ Class Template Specialization. Specialization is similar to c++ function template overloading. This can help in optimizing the templates for specific types. Once the template is specialized, all the member functions should be declared and defined for the specific data type.

Declaration – C++ Class Template Specialization:

Declaration should include the template keyword with the class name followed by the type in the angular brackets.

template<>
class MyQueue<double>
{
	std::vector<double> data;
	public:
	 void Add(double const &);
	 void Remove();
	 void Print();
};

The above C++ specialized template is the specialized version of the example used in the C++ Class Template article. This sample uses the double data type for specialization. Though this specific class mentioned here does not show any optimized code for the double data type, this can serve the purpose of understanding how a C++ class template specialization works.

Defining Functions for C++ Class Template Specialization:

Now all the functions for this specialized template should be declared individually for the double data type. Two of such functions are shown below.

template <> void MyQueue<double> ::Add(double const &d)
{
  data.push_back(d);
}

template <> void MyQueue<double>::Print()
{
	std::vector <double>::iterator It1;
	It1 = data.begin();
	cout<<"Double"<<endl;
	for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
	cout << " " << *It1<<endl;
}

If the above template MyQueue is used for double data type, the C++ compiler will not generate another version of class for the double data type from the generic MyQueue<typename T> template. Instead it will use the one defined as above in MyQueue<double>. For any other data types, the compiler will expand the generic c++ class template and use it.

Partial Specialization – C++ Class Template Specialization:

The above is the example for full specialization. Partial C++ template specializations are also possible. A good example code will be as follows.

template <typename T>
class MyQueue<T,T>{};

The above is the example for partial specialization with the same type.

template <typename T>
class MyQueue<T,double>{};

The above code is the example for partial specialization with a data type different from the template typename.

C++ Class Template specialization can be employed wherever it is believed that the code can be optimal if written with the specific data type. Otherwise a normal generic c++ class template can serve the purpose.