Template Specialization in C++ Program?

C++Server Side ProgrammingProgramming

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
Updated on 02-Mar-2020 11:11:13