Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Template specialization in C++

Reference for C++ template specialization: full and partial specialization of class and variable templates, function template alternatives and pitfalls.

Template specialization lets you provide a different implementation of a template for particular arguments. The primary template handles the general case; a specialization replaces it when the arguments match. A full (explicit) specialization fixes every template argument and is written template <>. A partial specialization fixes only some, or matches a pattern such as "any pointer type", and still has template parameters.

Class templates and variable templates support both kinds. Function templates support only full specialization; for partial behavior you overload instead. Specializations must be declared before the first use that would instantiate the primary template for those arguments.

C++
template <typename T> struct Name { static constexpr auto value = "other"; };
template <> struct Name<int> { static constexpr auto value = "int"; };
template <typename T> struct Name<T*> { static constexpr auto value = "pointer"; };

Try it

C++ Full and partial specialization

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++ Variable templates and function 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> struct S { };               // primary template
template <> struct S<int> { };                    // full specialization
template <typename T> struct S<T*> { };           // partial specialization
template <typename T, typename U> struct S<std::pair<T, U>> { };

template <> R f<int>(Params);                     // full specialization of a function template
template <typename T> constexpr bool v = false;   // variable template
template <> constexpr bool v<int> = true;
Kind Allowed for
Full (explicit) specialization Class, function, variable and member templates.
Partial specialization Class and variable templates only.
Overloading Function templates (the replacement for partial specialization).

Usage notes

  • Which one is chosen. The compiler picks the most specialized matching partial specialization. If two match equally well the call is ambiguous and the code does not compile.
  • A specialization is a different class. It does not inherit anything from the primary template; you have to repeat members, or put shared parts in a common base.
  • Declare before use. If code instantiates S<int> from the primary template and a specialization appears later, the program is ill-formed (no diagnostic required).
  • Function templates and overloads. Specializations of function templates do not take part in overload resolution; only the primary templates do. Then the specialization is used if the primary is selected. This surprises people: prefer overloads and if constexpr or concepts to specializations.
  • Full specializations of functions are ordinary functions. In a header they need inline, or one definition in a source file, otherwise you break the one-definition rule.
  • Members can be specialized separately (template <> void Box<int>::print() {}) without redefining the whole class.
  • Common standard-library specializations: std::hash<MyType>, std::formatter<MyType>, std::numeric_limits<MyType>. Specializing other std templates for standard types is undefined behavior; specializing them for your own types is only allowed for those meant for customization.
  • std::vector<bool> shows that a specialization may change the interface, which breaks generic code.
  • Constrained partial specialization is available with concepts: template <std::integral T> struct S<T> {}.
  • Type traits are built this way: std::is_pointer is a primary template inheriting false_type, plus a partial specialization T* inheriting true_type.