C++ Templates

    The template is one of the powerful tools of C++ and it is often used to eliminate the concept of function overloading where the function has a similar function definition, with the same number of parameters but different data types. For example, if we want to make a function that compares two objects and returns the largest one, for that we can write a simple if..else code inside a function. But the function would have some working limitation for example if we create a function that can compare two integers and return the largest number this means we can not use that same function for char and float datatype objects. To overcome this limitation C++ has two keywords “template” and “typename”. we can also use class keyword at typename place.

    Template in C++

    The template is a keyword that is used to define the function and class template in C++. With the help of a template, we make our function and class independent of any specific data type.

    • Function template
    • Class template

    Function Template:

    A function template is a special kind of function which is independent of its parameter’s datatype. With the function template, we can create a similar function definition for different datatypes. But make sure that the definite should be valid to the specific built-in data types. Syntax for Function Template:

    template <class placeholder>
    identifier function_name(placeholder parameter_1, placeholder parameter_2);


    template <typename placeholder>
    placeholder function_( placeholder parameter_1, placeholder parameter_2);

    Here the placeholder replaces the data type for the function and it is an identifier so you could give any name to it. The syntax for calling the function template:

    function_name <data_type > (arguments)

    We can either use typename or class for the function template. Example

    #include <iostream>
    using namespace std;
    template <typename X>
    X find_max(X a, X b)
        if (a>b)
           return a;
           return b;
    int main()
       cout<<find_max<int>(4,5)<<endl; // calling the function template



    Behind the Code In the above example, we have to use the same function for the different datatype. And when we call the function template in the main function, we also mention the argument datatypes after between the smaller than and greater than operators. Here X replace the data type for the function template, which means when we call the function find_max<int>(4,5) the X becomes int and when we call the function find_max<char>('q','g'); the X becomes char. So, in the above program with the help of a template, we create a function which was capable of working with different data type.

    Class Template

    Like the function template, we can create a class template which is also independent of the data type. The Class template often used in data structures such as linked list, Binary tree, Stack, Queue, etc. Class Template Syntax

    template <typename placeholder>
    class class_name
    //class body

    Creating Class Template Object Syntax

    class_name <datatype> object_name (constructor_arguments);


    #include <iostream>
    using namespace std;
    template <class T >
    class Employee {
        T a, b;
        Employee (T ID, T age)
            a=ID; b=age;
        T detail()
          cout<<"Id is: "<<a<<endl;
          cout<<"Age of Employee is: "<<b;
    int main () 
      Employee <int> sam (112244, 55);
      return 0;


    Id is: 112244
    Age of Employee is: 55


    • Template is a keyword which is used to create generic functions and class.
    • The placeholder act as the data type to the function and class template.

    People are also reading: