Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.
Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.
How template works?
Templates are expanded at compiler time. This is like macros. The difference is, compiler does type checking before template expansion. The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of same function/class.
Templates can be used in two different ways:
- Function Templates
- Class Templates
Function Templates:
We can define a template for a function that can be used for different data types.
Syntax of Function Template:
template <class T>
T someFunction(T arg1, T arg2, ...)
{
... .. ...
}
T
is a template argument that accepts different data types (int, float), and class is a keyword.
When, an argument of a data type is passed to someFunction( )
, compiler generates a new version of someFunction()
for the given data type.
Example 1: Function Template to find the largest number
/**
* Program to display largest among two numbers using function templates.
* If two characters are passed to function template, character with larger ASCII value is displayed.
**/
#include <iostream>
using namespace std;
// template function
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
}
/*
Output :
Enter two integers:
5
10
10 is larger.
Enter two floating-point numbers:
12.4
10.2
12.4 is larger.
Enter two characters:
z
Z
z has larger ASCII value.
*/
In the above program, a function template Large()
is defined that accepts two arguments n1
and n2
of data type T
. T
signifies that argument can be of any data type.
Large()
function returns the largest among the two arguments using a simple conditional operation.
Inside the main()
function, variables of three different data types: int
, float
and char
are declared. The variables are then passed to the Large()
function template as normal functions.
During run-time, when an integer is passed to the template function, compiler knows it has to generate a Large()
function to accept the int arguments and does so.
Similarly, when floating-point data and char data are passed, it knows the argument data types and generates the Large()
function accordingly.
This way, using only a single function template replaced three identical normal functions and made your code maintainable
Example 2: Swap Data Using Function Templates
/**
* Program to swap data using function templates.
**/
#include <iostream>
using namespace std;
template <typename T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main()
{
int i1 = 1, i2 = 2;
float f1 = 1.1, f2 = 2.2;
char c1 = 'a', c2 = 'b';
cout << "Before passing data to function template.\n";
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
cout << "\n\nAfter passing data to function template.\n";
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
return 0;
}
/*
Output :
Before passing data to function template.
i1 = 1
i2 = 2
f1 = 1.1
f2 = 2.2
c1 = a
c2 = b
After passing data to function template.
i1 = 2
i2 = 1
f1 = 2.2
f2 = 1.1
c1 = b
c2 = a
*/
In this program, instead of calling a function by passing a value, a call by reference is issued.
The Swap()
function template takes two arguments and swaps them by reference.