
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Example
#include <iostream> using namespace std; template <class T> void fun(T a) { cout << "The main template fun(): " << a << endl; } template<> void fun(int a) { cout << "Specialized Template for int type: " << a << endl; } int main(){ fun<char>('a'); fun<int>(10); fun<float>(10.14); return 0; }
Output
The main template fun(): a Specialized Template for int type: 10 The main template fun(): 10.14
- Related Articles
- Template Specialization in C++
- Template Metaprogramming in C++
- is_final template in C++
- is_fundamental Template in C++
- is_pod template in C++
- is_pointer Template in C++
- is_signed template in C++
- is_unsigned template in C++
- is_void template in C++
- is_standard_layout template in C++
- is_class template in C++
- is_const Template in C++
- is_lvalue_reference Template in C++
- is_rvalue_reference Template in C++
- is_empty template in C++

Advertisements