Summary: In this tutorial, we will learn what C++ Template is and how can we use it with functions and classes in C++.

What is a Template?

Template in C++ is a generic blueprint used to create a specialized class or function during compile time.

It makes functions or classes compatible to multiple data types.

For instance, suppose we want to create a function that add two numbers. The number can be of type int, float, or double.

To achieve this, we need to write multiple additional functions of different data types, which will increase the complexity and length of the program:

int add(int a, int b){
    return a+b;
}
 
double add(double a, double b){
    return a+b;
}
 
float add(float a, float b){
    return a+b;
}

We can replace the code above with a single template function, and it will work the same for all types of data.

How to Create a Template Function or Class?

To create a template function or class, we attach the template declaration statement before the function or class declaration and write the template parameter (T) in place of the type to make the code generic.

template<typename T>
<void or T> <function_name>(T a, T b,....){
    //code
}
 
//OR
 
template<class T>
<void or T> <function_name>(T a, T b,....){
    //code
}
 
//For More than one Data Types
 
template<typename T1, typename T2>
<void or T1 or T2> <function_name>(T1 a, T2 b,....){
    //code
}
 
//OR
 
template<class T1, class T2>
<void or T1 or T2> <function_name>(T1 a, T2 b,....){
    //code
}

Note: The class and typename are both valid. We can even use a primitive type in the template declaration as per our requirement.

Once we define the template, it is the job of the compiler to detect and provide the appropriate implementation during the compile time.

Let’s see some examples for better understanding.

Example 1: Function Template for Same Data Type

#include <iostream>
using namespace std;
 
template<typename T>
T add(T a, T b){
    return a+b;
}
 
int main()
{
    //Passing type as parameter
    cout << add<int>(9,10)<< endl;
    cout << add<double>(5.6,10.5)<< endl;
    cout << add<float>(9.58,8.9)<< endl;
 
    //Automatic detection
    cout << add(9,10)<< endl;
    cout << add(5.6,10.5)<< endl;
    cout << add(9.58,8.9)<< endl;
 
    return 0;
}

Output:

19
16.1
18.48
19
16.1
18.48

Example 2: Function Template for Different Data Types

#include <iostream>
using namespace std;
 
template<typename T1, typename T2>
T1 add(T1 a, T2 b){
    return a+b;
}
 
int main()
{
    //Passing type as parameter
    cout << add<int, double>(9,10.3)<< endl;
    cout << add<double, int>(5.6,10)<< endl;
    cout << add<float, int>(6.58,8)<< endl;
 
    //Automatic detection
    cout << add(9,10.3)<< endl;
    cout << add(5.6,10)<< endl;
    cout << add(6.58,8)<< endl;
 
    return 0;
}

Output:

19
15.6
14.58
19
15.6
14.58

Example 3: Class Template

Similar to the template function, a class can also be made generic with the help of the STL template.

#include <iostream>
using namespace std;
 
template <typename N, typename B>
class Account{
public:
    N holderName;
    B balance;
 
    Account(string name){
        balance = 1000;
        name = holderName;
    }
 
    void showBalance(){
        cout <<"Balance is: "<<balance<<endl;
    }
 
};
 
int main()
{
    Account<string,double> account("Programmer");
    account.showBalance();
}

Output:

Balance is: 1000

In this programming tutorial, we learned with examples the significance of the STL template in C++.

Leave a Reply