C++ Template Specialization

October 27, 2015 Posted by WithU Technologies
When we use regular templates for functions or classes. The
templates handle different data types in the same way as the same code is used
for compilation of all data types.

But in case we need a specific data type for a particular argument or parameter.
We use the method of Template specialization where we override the default template implementation to handle a particular type in a different way.

It allows for the definition of a different implementation of a template when a specific type overrides the regular template argument.

Class template specializations are treated as completely independent classes, even though they are allocated in the same way as the template class.

This means that we can change anything and everything about our specialization class, including the way it’s implemented and even the functions it makes public, just as if it were an independent
class.


A specialized template initialized without any parameters
like:

template<>   

The particular parameter for that template is declared after
the class name during initialization. 

class show<char>{  

This specialization parameter itself identifies
the type for which the template class is being specialized.

And after that the specified data type is used
throughout the specified template class.

Example:

#include <iostream>
using namespace std;

template<class R>  //at first we declared the regular
template with a parameter R

class show {   //Class
declared


public:

  show(int a) {   //Constructor initialized
with a parameter

            if(a==2483){            //if statement is used to generate an
output based on user input, which is an integer

                  cout<<"You are right!"<<endl;

                  }

                  else{

                        cout<<"You are wrong!"<<endl;

                  }

            }

};

template<>    //Specialized
template is initialized without any parameters

class show<char>{   //Data type is
declare just after the same class name, which is declared earlier with regular
template

public:

      show(char a){   //Constructor also use that specialized
data


            //if statement
is used to generate an output based on user input, which a character

            if(a=='T' || a=='t'){

            cout<<"You are right!"<<endl;

            }

            else{

                  cout<<"You are wrong!"<<endl;

            }

      }

};

int main() {

    int s;    //local
variable initialized

      cout<<"Enter
your Choice? \n Question 1\n Question 2"
<<endl;   //to choose an
one question we implement if statements

      cin>>s;

      if(s==1){

            char x;

                          cout<<"Only 8%
of the world's currency is physical money, the rest only exist on
computers."
<<endl;

                              cout<<"(Enter T/F for True or
False)"
<<endl;

                              cin>>x;

                              show<char>Newob1(x);   //Char data type is also mentioned
here after class name and argument x is passed

      }

      if(s==2){

            int y;

            cout<<"Jerry sells mangoes in the
market. \nFirst day he sells 1458 apples. \nSecond day he sells 1025 apples. \nOn
the third day, he sells 126 less than the second day. \nHow
many apples are sold in three days "
<<endl;

            cin>>y;

            show<int>Newob2(y);
//int data type is also
mentioned here after class name and argument y is passed

      }

      return 0;

}


Here in the example given above a simple program
is written where at first we ask the user to choose any one question from two.

After that we provide the selected question and
took input from user and then generate an output according to that.

First of all, a regular template is initialized
to generate output based on integer datatype which is already learned by us.

But then we declared a specialized template with
the same class name to distinguish the regular template used for integers from
characters specialized template.

And we used the character specialized template
ta take a character input from user and output result according to that.