
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Template Specialization in C++
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
Advertisements