Learning Hub Search Ctrl K

Legacy C++ › 4. Templates and Namespaces

Template Specialization

Full and partial specialization, and basic traits techniques.

advanced #templates#specialization#partial-specialization#traits

Explanation

A template describes the general case; specialization supplies a different implementation for particular arguments.

  • Full (explicit) specialization: template <> class Printer<bool> { ... }; replaces the template for exactly that argument list. Works for classes and functions.
  • Partial specialization: template <typename T> class Printer<T*> { ... }; matches a family of types (all pointers). Allowed for class templates only; for functions use overloading instead.

A traits class uses specialization to attach compile-time facts to types, with no run-time cost. Pre-C++11 code uses this heavily (std::iterator_traits, std::char_traits).

Example

#include <iostream>
#include <string>

// primary template
template <typename T>
struct TypeInfo {
    static const char* name() { return "unknown"; }
    static const bool  isPointer = false;
};

// full specializations
template <> struct TypeInfo<int>    { static const char* name() { return "int"; }    static const bool isPointer = false; };
template <> struct TypeInfo<double> { static const char* name() { return "double"; } static const bool isPointer = false; };

// partial specialization for any pointer type
template <typename T>
struct TypeInfo<T*> {
    static const char* name() { return "pointer"; }
    static const bool  isPointer = true;
};

template <typename T>
void describe(const T&) {
    std::cout << TypeInfo<T>::name() << (TypeInfo<T>::isPointer ? " (is pointer)" : "") << "\n";
}

// function template with a full specialization
template <typename T> std::string toText(const T& v)     { return "value"; }
template <>           std::string toText(const bool& v)  { return v ? "yes" : "no"; }

int main() {
    int i = 0; double d = 0; int* p = &i; std::string s;
    describe(i);      // int
    describe(d);      // double
    describe(p);      // pointer (is pointer)
    describe(s);      // unknown
    std::cout << toText(true) << " " << toText(3) << "\n";
    return 0;
}

Common pitfalls

  • Specializations must be declared before the first use that would instantiate the primary template.
  • Function template specializations do not take part in overload resolution; prefer overloads over specializing function templates.
  • A partial specialization that is not more specialized than the primary template is an error.
  • In a header, a full function specialization is not implicitly inline; mark it inline or you get multiple-definition errors.
  • Ambiguity arises if two partial specializations both match with neither more specialized.

Legacy vs modern

C++11 <type_traits> (std::is_pointer<T>, std::enable_if) packages the traits you used to write by hand; C++17 if constexpr and C++20 concepts often replace specialization tricks. See Concepts and Constraints.

Exercises

1. Why can you partially specialize a class template but not a function template?

The language only allows it for classes; function overloading already provides the same flexibility with simpler rules.