C++ templates

In this tutorial, we will discuss one of the most powerful features of C++ programming named templates. There are two types of C++ templates:

  • function templates
  • class templates

Especially in software development, templates allow development of reusable components with functions and classes, supporting different data types in the single framework. Thus template supports generic programming.

In C++, functions and classes can only manipulate or operate on specific data. For example, a function can either work on integer type data or floating type data at a time. But with templates, a function can work on different data types of similar operation.

C++ Function Templates


In C++, a normal function can operate on the single type of data only.

For example:

void addition(int a, int b) { ...... }

This addition function can only operate on integer type of data and if we were to operate on floating point data types, we have to write another function like:

void addition(float a, float b) { ...... }

Overloaded functions can operate on the identical operation for different types but that increases the length of the program.

To address such problems, a C++ function template or generic function can be used to operate on different data types by taking data type as a parameter.

C++ function templates: syntax

template 
return_type function_name(parameters)
{
   //function body
}

All function template definitions begin with keyword template followed by a list of template parameters to the function template enclosed in angle brackets( < and > ). The keyword class signifies a generic data type ElementType.

Example: C++ program to illustrate concept of function template

//function_template.cpp
#include <iostream>
using namespace std;

//defining generic template function
template <class T>
T add_num ( T a, T b )
 {
   return (a+b);
 }

int main()
 {
   cout << "Output1 = " << add_num(2,4) << "\n";
   cout << "Output2 = "<< add_num(2.3,3.7) << "\n"; 
  return 0;
 }

Output

c++ templates

Explanation
As seen in above example, we have defined a generic template function which can operate on both integer type data and floating point type.

C++ class templates


Similar to that of function templates, a class that can operate on any type of data is called class template.

The keyword template is used before the declaration of class as well to signify the declaration of a class template. The syntax for declaring class template is:

//class template declaration
template <class template_type, .. >
class class_name
{
  private:
    // data members
  public:
    // member functions
}
int main ()
{
   //instantiation of class template
  class_name <data_type1, data_type2,...> object;
}

Note that the generic datatype declared while declaring class template is replaced by the datatypes supplied by the user while instantiating the class.

Example: C++ program to illustrate the concept of class template

//class_template.cpp
#include <iostream>
using namespace std;

template<class t1,class t2>
class Example
{
    t1 x;
    t2 y;
    public:
        void add(t1 x, t1 y)
        {
            cout << "\nThe sum is " << x + y << "\n";
        }
};

int main()
{
    Example<int,int> obj1;
    Example<int,float> obj2;
    obj1.add(1,1);
    obj2.add(2.2,3.1);
    return 0;
}

Output

template