Skip to content
Tutorials Center
My learning

Topics

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.

C++
template <typename T, std::size_t N>
T sum(const T (&a)[N]);

template <typename T>
struct Box { T value; };

Try it

C++ Function, class, variable and alias 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.

C++ Specialization and explicit instantiation

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 <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 T can 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 template plus one explicit instantiation in a .cpp file reduces build time.
  • Function templates cannot be partially specialized. Use overloads or if constexpr instead; 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 typename for dependent types and template for dependent member templates (obj.template get<0>()).
  • Within a class template, the injected class name Box means Box<T>.
  • Every distinct set of arguments is a different type: Buffer<char, 3> and Buffer<char, 4> are unrelated classes.
  • Templates are not templates of source text: T x = 0; for T = std::string fails 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.