C++ Reference ยท Keywords and Statements
The template keyword
Reference for C++ template: function, class, variable and alias templates, type and non-type parameters, defaults, explicit instantiation and specialization.
template introduces a family of functions, classes, variables or aliases that the compiler stamps out for particular arguments. You write the code once with placeholders (template parameters), and each use with concrete arguments produces a separate, fully type-checked instantiation. There is no runtime cost for the abstraction: std::vector<int> and std::vector<double> are two ordinary classes.
Template parameters can be types (typename T), values (int N, or any structural type since C++20), or other templates. Since C++20 a parameter can also be constrained by a concept, and auto can stand in for a non-type parameter.
template <typename T, std::size_t N> T sum(const T (&a)[N]); template <typename T> struct Box { T value; };
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 <parameter-list> declaration template <parameter-list> requires constraint declaration // C++20 template <> declaration // full specialization template declaration; // explicit instantiation definition extern template declaration; // explicit instantiation declaration
| Parameter kind | Example | Description |
|---|---|---|
| Type | typename T, class T |
typename and class mean the same here. |
| Non-type | int N, auto V, bool Flag |
A compile-time value. Integers, enums, pointers, references and (C++20) literal class types with only public members. |
| Template template | template <class> class C |
A parameter that is itself a class template, such as a container. |
| Parameter pack | typename... Ts |
Zero or more arguments (variadic templates). |
| Default | typename T = int |
Used when the argument is omitted. Defaults may refer to earlier parameters. |
Usage notes
- A function template's arguments are usually deduced from the call; a class template's arguments are deduced from constructors since C++17 (CTAD). You can always give them explicitly.
- A template is only compiled for real when it is instantiated. Errors that depend on
Tcan hide until then, which is why concepts (requires) give much better messages. - Definitions normally live in headers, because the compiler needs the body at the point of instantiation.
extern templateplus one explicit instantiation in a.cppfile reduces build time. - Function templates cannot be partially specialized. Use overloads or
if constexprinstead; class and variable templates can be partially specialized. - Inside a template, a name that depends on a parameter is assumed to be a value; write
typenamefor dependent types andtemplatefor dependent member templates (obj.template get<0>()). - Within a class template, the injected class name
BoxmeansBox<T>. - Every distinct set of arguments is a different type:
Buffer<char, 3>andBuffer<char, 4>are unrelated classes. - Templates are not templates of source text:
T x = 0;forT = std::stringfails at that line, and the error appears at the instantiation. - Abbreviated function templates (C++20):
void f(auto x)is a function template with an invented parameter.