The template is a helpful feature that has been added to C++. This new concept allows programmers to define standard classes and functions and thus provide support for general 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 types and data structures. 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. This tutorial will teach you how to use templates within a C++ program.
What are Templates in C++?
Templates are primarily 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 a formula or blueprint for generic class or function creations. It allows a function or class to work on different data types without rewriting them.
- 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 the 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. It defines a family of functions. No program is generated from its source file, containing only template definitions. So, for showing a code, a template must be instantiated, i.e., the template arguments must be established so that the compiler can generate an actual function (or class, from a class template).
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:10 12
Please insert 2 Character Values:A B
Character Values after Swapping:B A
Please insert 2 Float Values:1.1 2.1
The resultatnt float values after swapping:2.1 1.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 that became the template name, and the parameter 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, 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 either by the template function or by ordinary functions of its name. In such programming cases, the overloading resolution is accomplished as follows:
- Call a standard function that has an exact match
- A template function is called that could be created with an exact match
- Try normal overloading resolution to standard functions and call the one that matches
Disadvantages of Using Template
- Some compilers have poor support for templates.
- Many compilers lack clear instructions when they detect errors in the definition of the template.
- Many compilers do not support the nesting of templates.
- When templates are used, all codes get exposed.
- The templates are in the header, where the complete rebuild of all project pieces is required when the changes occur.