Templates
Templates
Templates in C++
A template is a simple yet very powerful tool in C++. The simple idea
is to pass data type as a parameter so that we don’t need to write the
same code for different data types. For example, a software company
may need to sort() for different data types. Rather than writing and
maintaining multiple codes, we can write one sort() and pass data
type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and
‘typename’. The second keyword can always be replaced by the
keyword ‘class’.
Templates are expanded at compiler time. This is like macros. The
difference is, that the 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 the
same function/class.
Function Templates
We write a generic function that can be used for different data types.
Examples of function templates are sort(), max(), min(), printArray().
Function Template Example
#include <iostream>
using namespace std;
// One function works for all data types.
template <typename T> T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0)
<< endl; // call myMax for double
cout << myMax<char>('g', 'e')
<< endl; // call myMax for char
return 0;
Class Templates
size = s; return 0;
ptr[i] = arr[i];
}
What is the difference between
function overloading and
templates?
Both function overloading and templates are examples of polymorphism
features of OOP. Function overloading is used when multiple functions do
quite similar (not identical) operations, templates are used when
multiple functions do identical operations.