C++ Function Templates
In this page:
template <typename T>
T function_name(T param1, T param2) {
return expression;
}
function_name<type>(arg1, arg2); // explicit type
function_name(arg1, arg2); // type deduced
Defining Function Templates
A function template defines a generic function blueprint: you write the template keyword followed by a list of type parameters in angle brackets, then a normal-looking function signature that uses those parameter names in place of concrete types.
Example: Defining Function Templates
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T square(T x) {
// Send x * x back to the caller
return x * x;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << square(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Calling Function Templates
You can call a function template explicitly by specifying the type inside angle brackets yourself, or simply call it like a normal function and let the compiler deduce the type automatically from the arguments you pass — the latter is far more common in practice.
Example: Calling Function Templates
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T square(T x) {
// Send x * x back to the caller
return x * x;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << square<double>(2.5) << std::endl;
// Print to the console with cout
std::cout << square(4) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Template Parameters
A single function template can declare multiple type parameters, letting one generic function handle several arguments of genuinely different, unrelated types in the same call rather than requiring them all to match.
Example: Multiple Template Parameters
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T1, typename T2>
void showPair(T1 a, T2 b) {
// Print to the console with cout
std::cout << a << " " << b << std::endl;
}
// Program execution starts in main()
int main() {
showPair(5, "hello");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading Function Templates
You can overload a function template just like an ordinary function, either by providing a specific non-template overload for a particular type or by defining another template with a different number of parameters, and the compiler picks the best match.
Example: Overloading Function Templates
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T maxValue(T a, T b) {
// Send (a > b) ? a : b back to the caller
return (a > b) ? a : b;
}
int maxValue(int a, int b, int c) {
// Send maxValue(maxValue(a, b), c) back to the caller
return maxValue(maxValue(a, b), c);
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << maxValue(3, 7) << std::endl;
// Print to the console with cout
std::cout << maxValue(3, 7, 5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Non-Type Template Parameters
Templates also accept non-type parameters — constant values like integers rather than types — which the compiler treats as compile-time constants inside the function body, useful for things like fixed buffer sizes baked in at instantiation.
Example: Non-Type Template Parameters
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <int N>
int multiplyByN(int x) {
// Send x * N back to the caller
return x * N;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << multiplyByN<3>(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Passing arguments of different types like
max(3, 4.5)and getting a deduction error, since both must give the sameT. - Forgetting
template <typename T>above the function. - Using an operation that the type does not support, such as
<on a type without it.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: