6.2) Function templates in C++
Function templates in C++ are a mechanism that allows you to create generic functions capable of working with multiple data types. Instead of writing separate function implementations for each data type, you can define a single function template that adapts to the specific data types used when the function is called.
Here’s a detailed explanation of function templates in C++:
Table of Contents
1. Syntax of Function Templates
A function template is defined using the template keyword followed by a template parameter list enclosed in angle brackets (<>
). The template parameter(s) act as placeholders for the data types to be used in the function.
template <typename T>
T functionName(T parameter) {
// Function implementation
}
2. Using Function Templates
When you call a function template, you specify the desired data types within angle brackets (<>
) right after the function name. The compiler then generates a version of the function specifically for those data types.
int main() {
int result1 = functionName<int>(5);
double result2 = functionName<double>(2.5);
return 0;
}
3. Template Type Deduction (C++17 and later)
In C++17 and later, you can omit the template argument when calling a function template if the compiler can deduce the type from the function arguments.
int main() {
auto result1 = functionName(5); // Deduces T as int
auto result2 = functionName(2.5); // Deduces T as double
return 0;
}
4. Overloading and Function Templates
Function templates can coexist with regular overloaded functions. The compiler will choose the appropriate version based on the argument types.
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
5. Multiple Template Parameters
Function templates can have multiple template parameters for more complex scenarios, such as using different data types.
template <typename T, typename U>
T multiplyAndAdd(T a, U b) {
return a * 2 + b;
}
Benefits of Function Templates
- Code Reusability: Function templates eliminate the need to write separate functions for each data type.
- Flexibility: You can create generic functions that work with different data types.
- Standard Library: The C++ Standard Library extensively uses function templates to provide generic algorithms and data structures.
Function Template Example
Let’s consider a basic example of a function template for finding the maximum of two values:
#include <iostream>
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int intMax = max(5, 3);
double doubleMax = max(2.5, 1.5);
std::cout << "Max int: " << intMax << std::endl;
std::cout << "Max double: " << doubleMax << std::endl;
return 0;
}
Output:
Max int: 5
Max double: 2.5
Function templates are a fundamental feature of C++ that promote code reusability and maintainability by allowing you to create generic functions that work with a variety of data types. They are widely used in C++ programming to build flexible and efficient libraries, algorithms, and container classes.