Template Specialization in C++ Program?

In this tutorial, we will be discussing a program to understand Template specialization in C++.

Standard functions like sort() can be used with any data types and they behave the same with each of them. But if you want to set a special behaviour of the function for a particular data type (even user defined), we can use template specialization.


 Live Demo

#include <iostream>
using namespace std;
template <class T>
void fun(T a) {
   cout << "The main template fun(): " << a << endl;
void fun(int a) {
   cout << "Specialized Template for int type: " << a << endl;
int main(){
   return 0;


The main template fun(): a
Specialized Template for int type: 10
The main template fun(): 10.14