C++ operates as a federation of languages, where templates play a crucial role in enabling generic programming. Templates address a fundamental challenge: when you have similar operations that differ only in data types, instead of writing separate functions for each type, templates provide a unified solution.
Function Template Basics
Simple Function Template Example
Function templates enable behavior that works across multiple data types. Rather than restricting functionality to specific types, templates offer universal solutions applicable to various data structures.
Consider this basic example:
// Template function to return the larger of two values
template <typename ElementType>
inline ElementType const& find_maximum(ElementType const& first, ElementType const& second) {
return first < second ? second : first;
}
This template accepts two parameters and uses ElementType as a placeholder for the actual type. The syntax follows the pattern: template <parameter-list> where parameters are separated by commas.
The naming convention typically uses single letters like T, U, or V, though any identifier works.
Here's a practical demonstration:
#include <iostream>
#include <string>
using namespace std;
template <typename ElementType>
inline ElementType const& find_maximum(ElementType const& first, ElementType const& second) {
return first < second ? second : first;
}
int main() {
int num1 = 42, num2 = 40;
cout << "find_maximum(num1, num2): " << find_maximum(num1, num2) << endl;
double val1 = 1.23, val2 = 4.56;
cout << "find_maximum(val1, val2): " << find_maximum(val1, val2) << endl;
string text1 = "hello", text2 = "world";
cout << "find_maximum(text1, text2): " << find_maximum(text1, text2) << endl;
return 0;
}
Output:
find_maximum(num1, num2): 42
find_maximum(val1, val2): 4.56
find_maximum(text1, text2): world
...Program finished with exit code 0
Press ENTER to exit console.
The find_maximum function was invoked three times with different parameter types: int, double, and string. Template functions don't create a single entity that handles all type. Instead, each unique type combination generates a distinct function instance from the template.
When integer parameters are passed, the compiler automatically generates:
const int& find_maximum(int const&, int const&);
inline int const& find_maximum(int const& first, int const& second) {
return first < second ? second : first;
}
This process of substituting concrete types for template parameters is called instantiation.
Template compilation occurs in two phases:
- Before instantiation: Verifies template syntax correctness
- During instantiation: Ensures all member calls are valid
If the template contains errors or attempts invalid operations, compilation will fail during these phases.
Template Parameter Categories
Function templates work with two parameter types:
(1) Template Parameters: Declared in angle brackets before the function name
template <typename ElementType>
(2) Call Parameters: Declared in parentheses after the function name
find_maximum(ElementType const& first, ElementType const& second)
In practice, mixed parameter types can cause issues. Templates perform exact type matching without automatic conversion. For example, passing int and double parameters won't trigger automatic type conversion within the template context.