Template Specialization in C++

In this tutorial, we will get all the information about template specialization in C++. Template is a feature of C++. Templates are used to create the generalized function and class. The data type int, char, string, float, and user-defined data types are used using the template.

Now, before moving ahead to this topic we must know what a template is.
A template is a feature available in C++. It enables us to use the same code for different data types, not excluding user-defined data types. Using a template we can use, a created class stack, like a stack of any data type.
Now, it can come to mind that what if somebody wants different code for some particular data type. Is it possible while using the template feature? The answer is yes. Here comes the picture of the template specialization feature. Using template specialization in C++ we can perform different operations for a particular data type.
For example:

Consider you want to use heap sort for an array of any data type except the array 
of the char data type. For an array of char data type counting sort will be more 
suitable as there are only 256 characters as of total. This situation can be 
implemented using the template specialization feature of C++.

Template Specialization

Template specialization is the property of c++ in which a special behavior for a particular data type is created.
Template specialization can be done using function and class for any data type int, float, double, char, and user-defined.

Example of Specialized Function template

An Example Program for function template specialization
In this example, we have simple code for the function template in which we have a generalized template for char, float, and string but specialized template for int.

#include<iostream>
using namespace std;
template<typename T>
void name(T x) {
   cout << "This is generalized template"<<endl;
   cout<<"The value is: " << x << endl;
}
/// specialized template  function only for characters
template<>
void name(int x) {
   cout << "This is specialized template"<<endl;
    cout<<"The value is: " << x << endl;
}
main() {
   name<int >(120);
    name<char>('N');
   name<float>(9.6);
  name<string>("wow");
}

 

 

Output:

This is specialized template
The value is: 120
This is generalized template
The value is: N
This is generalized template
The value is: 9.6
This is generalized template
The value is: wow

Another example:

The code below shows how template specialization in C++ works.

#include <iostream> 
using namespace std; 
  
template <class X> 
void sort_array(X ar[], int n) 
{ 
    cout<<"Heap sort is performed here!!!!!"<<endl;
} 
  
// Template Specialization implementation
template <> 
void sort_array<char>(char ar[], int n) 
{  
    cout<<"Counting sort is performed here!!!!!"<<endl;
} 
int main() 
{ 
  int a[]={4,2,6,1,8};
  char c[]={'c','x', 'a', 't', 'b'};
  float f[]={3.1, 4.6, 1.1, 8.7};
  int s1,s2,s3;
   s1 = sizeof(a) / sizeof(a[0]);
   s2 = sizeof(c) / sizeof(c[0]);
   s3 = sizeof(f) / sizeof(f[0]);
    sort_array<int>(a,s1); 
    sort_array<char>(c,s2); 
    sort_array<float>(f,s3); 
}
The output of the following program is:
Heap sort is performed here!!!!! 
Counting sort is performed here!!!!! 
Heap sort is performed here!!!!!

In this way, we can see how we can perform the different operations for some particular data types using template specialization.

Example of Specialized Class Template in C++

An Example Program for class template specialization
In this example, we have simple code for the class in which we have a generalized template for char, float, and int but a specialized template for string.

#include<iostream>
using namespace std;
template<typename T>
///normal class
class name {
   public:
      name() {
         cout << "call from  generalized class " << endl;
      }
};
/// specialized class only for string
template<>
class name <string >{
   public:
      name() {
         cout << "call from  specialized class  " << endl;
      }
};
main() {
   name<int> obj_int;
   name<string> obj_string;
   name<float> obj_float;
   name<char> obj_char;

}
output:
call from generalized class
call from specialized class
call from generalized class
call from generalized class

Leave a Reply

Your email address will not be published. Required fields are marked *