Skip to content
Tutorials Center
My learning

Topics

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.

C++
template <typename T>
T maxOf(T a, T b) { return b < a ? a : b; }

Try it

C++ Deduction and explicit arguments

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.

C++ Overloads, non-templates and abbreviated templates

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

C++
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 type T that receive different argument types make deduction fail.
  • Deduction rules for by-value T: top-level const and references are dropped, arrays and functions decay. For T& the argument must be an lvalue and keeps its const. For T&& (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 .cpp file 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 needs typename.
  • 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, and extern template suppresses it elsewhere.