C++ Reference ยท Types and Language Concepts
Function templates in C++
Reference for C++ function templates: template parameters, argument deduction, explicit arguments, overloading with non-templates, abbreviated templates.
A function template is a recipe for a family of functions. You write the code once with placeholder types or values, and the compiler creates (instantiates) a real function for each combination of arguments you use. Unlike a macro, a template is type-checked and follows the normal scope and overload rules.
Template arguments are usually deduced from the call arguments, so you write maxOf(3, 8) and not maxOf<int>(3, 8). You can also give them explicitly, which is needed when a parameter cannot be deduced, such as a return type.
template <typename T> T maxOf(T a, T b) { return b < a ? a : b; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <typename T, typename U = T, int N = 4> R name(Params); template <typename... Ts> void f(Ts... args); // variadic auto g(auto x, auto y); // abbreviated: one hidden template parameter per auto
| Template parameter | Description |
|---|---|
typename T / class T |
A type parameter (the two keywords mean the same here). |
int N, auto V |
A non-type parameter: integral, enum, pointer, reference, floating point or class type with structural rules (C++20). auto V deduces the type (C++17). |
template <typename> class C |
A template template parameter. |
typename... Ts |
A parameter pack (see variadic templates). |
| Default argument | typename U = T. Function templates can have defaults since C++11. |
Instantiation errors appear when the body uses an operation the type does not support, such as < on a type with no such operator.
Usage notes
- Deduction uses the call arguments only. The return type, default arguments and braced lists (
{1, 2}) do not take part. Parameters of typeTthat receive different argument types make deduction fail. - Deduction rules for by-value
T: top-levelconstand references are dropped, arrays and functions decay. ForT&the argument must be an lvalue and keeps itsconst. ForT&&(forwarding reference) see forwarding references. - Overload resolution prefers a non-template function over a template when the conversions are equally good, and a more specialized template over a less specialized one. Function templates cannot be partially specialized; use overloads (see template specialization).
- Definitions go in headers (or a module), because the compiler needs the body at the point of instantiation. A template defined only in a
.cppfile causes linker errors for other files. - Constrain templates with concepts (C++20) for readable errors:
template <std::integral T> T gcd(T a, T b). - Code size. Every distinct argument list is a separate function. Many instantiations mean a bigger binary and slower builds.
- Two-phase lookup. Names that do not depend on a template parameter are looked up at definition; dependent names (
T::value_type) at instantiation, and a dependent type needstypename. - Return-type deduction (
auto f(T x)) and trailing returns (-> decltype(a + b)) are the tools when the result depends on the arguments. - Taking a function template's address needs the arguments to be determined:
auto p = &maxOf<int>;. - Explicit instantiation (
template int maxOf<int>(int, int);) forces code generation in one file, andextern templatesuppresses it elsewhere.