Templates in C++

Template is another essential feature added recently to C++. This new concept allows programmers to define generic classes and functions and thus provide support for generic programming. Generic programming is an approach of C++ programming where generic types are used as parameters so that they can work for various cases of suitable data type and data structure. The template is the basis for establishing the concept of generic programming, which entails writing code in a way that is independent of a particular type. So, in this chapter, you will learn about how to use templates within a C++ program along with its structure and syntax.

What are Templates in C++?

Templates are mostly implemented for crafting a family of classes or functions having similar features. For example, a class template for an array of the class would create an array having various data types such as float array and char array. Similarly, you can define a template for a function that helps you to create multiple versions of the same function for a specific purpose.

A template can be considered as a type of macro; When a particular type of object is defined for use, then the template definition for that class is substituted with the required data type. A template can be considered as a formula or blueprints for generic class or function creations. This allows a function or class to work on different data types, without having to re-write them again for each.

Templates can be of two types in C++:
  • Function templates
  • Class templates

Function Templates

A function template defines a family of functions.

Syntax:
template < parameter-list > function-declaration

export template < parameter-list > function-declaration

where function-declaration is the function name declared that becomes a template name and parameter-list is a non-empty comma-separated list of the template parameters.

The general form of a function template is:

Syntax:
template <class type> ret-type func-name(parameter list)
{
    // body of function
}

Function Template Instantiation

Function template plays a significant role, but it is neither a type by itself nor a function alone. It is not even an entity. They define a family of functions. No program gets generated from its source file which contains only template definitions. So, for showing a code, a template must have to be instantiated, i.e., the template arguments must have to be established so that the compiler can generate an actual function (or class, from a class template).

This is the syntax for explicit instantiation:

Syntax:
template returnType nameOfTemplate <listOfArgument> (listOfParameter);

Here is an example of a template in C++:

Example:
#include<iostream.h>
#include<conio.h>
template<class swap>

void swapp(swap &i, swap &j)
{
    swap t;
    t=i;
    i=j;
    j=t;
}
 
int main() {
    int e,f;
    char g,r;
    float x,y;

    cout<<"\n Please insert 2 Integer Values:"; cin>>e>>f;
    swapp(e,f);
    cout<<"\nInteger values after Swapping:";
    cout<<e<<"\t"<<f<<"\n\n";
    
    cout<<"\n Please insert 2 Character Values:"; cin>>g>>r;
    swapp(g,r);
    cout<<"\n Character Values after Swapping:";
    
    cout<<g<<"\t"<<r<<"\n\n";
    cout<<"\n please insert 2 Float Values:"; cin>>x>>y;
    swapp(x,y);
    cout<<"\n The resultatnt float values after swapping:";
    cout<<x<<"\t"<<y<<"\n\n";
}
Output:
Please insert 2 Integer Values: 12 10
Integer values after Swapping:1012

Please insert 2 Character Values:A B
Character Values after Swapping:BA

Please insert 2 Float Values:1.1 2.1
The resultatnt float values after swapping:2.11.1

Class Template

A class template defines a family of classes.

Syntax:
template < parameter-list > class-declaration

or

export template < parameter-list > class-declaration

Where a class declaration is the class name which became the template name. Parameter - the list is a non-empty comma-separated list of the template parameters.

Instantiation of Class Template

A class template by itself is not a type, or an object, or any other entity. No code is generated from a source file that contains only template definitions.

This is the syntax for explicit instantiation is:

Syntax:
template class name < argument-list >;       // Explicit instantiation definition

extern template class name < argument-list >;// Explicit instantiation declaration

Overloading of Template Function

A template function may be overloaded wither by template function or by ordinary functions of its name. In such programming cases, the overloading resolution is accomplished as follows:

  1. Call an ordinary function that has an exact match
  2. A template function is called that could be created with an exact match
  3. Try normal overloading resolution to ordinary functions and call the one that matches

Disadvantages of Using Template

  1. Some compilers have poor support of template.
  2. Many compilers lack clear instructions when they detect the error in the definition of the template.
  3. Many compilers do not support nesting of templates.
  4. When templates are used, all codes get exposed.
  5. The templates are in the header, in which the complete rebuild of all project pieces is required when the changes occur.