Template Specialization in C++
Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a data type sort() which is used everywhere in the code, and there is also a class called stack() which is used in every stack data type. There is a Quick shot class that is used for every data type except char. Let there is a big project where we need sort() for every data type, but if we use sort() for every data type, then it may affect the readability of the code. So to overcome this problem, C++ has a unique feature that we create special behaviour for a particular data type. This is called template specialization.
With the help of template specialization, we can use generic functions and generic classes in C++ programming, and it also provides the support of generic programming.
In C++, when we use a generic data type in an algorithm, then this programming is known as generic programming. With the help of generic programming, the same code is used in different ways.
Sometimes, we call Template as parameterized classes or functions.
Let us recall the concept of function overloading with the help of the below example.
Example 1:
#include<iostream>
using namespace std;
void show(int,int);
void show(double,double);
void show(char,char);
main()
{
show(3,4);
show(4.6,8.9);
return 0;
}
void show(int p,int q)
{
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
}
void show(double p,double q)
{
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
}
Output:
When we carefully see the above code, we observe that the overloaded function in our program shows the disadvantages of the overloaded function. The disadvantage is each overloaded function performs the same work. But the difference is each overloaded function handles the different data type argument. So, it is a disadvantage for the C++ program. After knowing these disadvantages, a new concept came into existence in C++. This new concept is called “Function Template”.
Function Template
With the help of a function template, we can write generic function in the C++ code that is not dependent on data type. We can also reduce the size of the code by using a function template.
Syntax:
template <class T>
<return-type><function-name> ( <parameters of type T> )
{
//function body
}
Where
template ------ keyword
class T ------ template type parameter enclosed within a pair of angle brackets(<>) called generic dt.
Example 2:
#include<iostream>
using namespace std;
template<class X>
void show(X p,X q)
{
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
}
main()
{
show(3,4);
show(4.6,8.9);
return 0;
}
Output:
Let us find the maximum of two values by using the function template.
Example 3:
#include<iostream>
using namespace std;
template<class Z>
void getMax(Z p,Z q)
{
Z result;
result=(p>q)?p:q;
cout<<endl<<"Maximum Among Two is:"<<result;
}
main()
{
getMax(4,7);
getMax(3.6,8.6);
getMax('H','L');
return 0;
}
Output:
Example 4:
#include<iostream>
using namespace std;
template<class Z>
Z getMax(Z p,Z q)
{
Z result;
result=(p>q)?p:q;
return result;
}
main()
{
int p=getMax(4,7);
double q=getMax(3.6,8.6);
cout<<endl<<p;
cout<<endl<<q;
return 0;
}
Output:
Let us take an example of function template specialization. We take the fun() for the general Template.
Example 5:
#include <iostream>
using namespace std;
template <class Z>
void fun(Z p)
{
cout <<"The main template for fun(): "
<< p << endl;
}
template<>
void fun(int p)
{
cout <<"Specialized Template for int type: "
<< p << endl;
}
int main()
{
fun<char>('p');
fun<int>(21);
fun<float>(8.14);
}
Output:
Example of class template specialization
#include <iostream>
using namespace std;
template <class Z>
class Test
{
public:
Test()
{
cout <<"It is a General template object \n";
}
};
template <>
class Test <int>
{
public:
Test()
{
cout <<"It is a Specialized template object\n";
}
};
int main()
{
Test<int> p;
Test<char> q;
Test<float> r;
return 0;
}
Output:
How does template specialization work?
After writing any Template based class, the compiler just copies that function and then uses that function on a new data type. The compiler first checks whether there is any specialized version available or not.