Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

The curiously recurring template pattern (CRTP)

Reference for the C++ CRTP idiom: static polymorphism, mixins that add operators, deducing this as the modern alternative, and the common pitfalls.

The curiously recurring template pattern (CRTP) is a C++ idiom in which a class derives from a class template and passes itself as the template argument: class Circle : public Shape<Circle>. The base class then knows the exact type of the derived class at compile time and can call its members through a static_cast.

CRTP gives you static polymorphism: behavior is selected during compilation, with no virtual table and no indirect call, so the compiler can inline everything. It is also the standard way to write a mixin: a base that adds functionality (such as comparison operators or a clone() function) to any class that derives from it.

C++
template <typename Derived>
struct Base {
    void hello() { static_cast<Derived&>(*this).impl(); }
};
struct Impl : Base<Impl> { void impl() {} };

Try it

C++ Static polymorphism

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++ A mixin that adds operators

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 Derived>
class Base { /* may use static_cast<Derived&>(*this) */ };

class Derived : public Base<Derived> { /* ... */ };
Name Description
Derived The class that inherits from the base. It is incomplete when Base<Derived> is instantiated as a base class, so the base cannot use its members in the class body, only inside member function bodies.
static_cast<Derived&>(*this) Converts the base to the derived type without a runtime check. Valid only if the object really is a Derived.

Errors / undefined behavior: if a class passes the wrong type (class A : Base<B>) and then the base casts to B&, the cast is undefined behavior. Making the base constructor private and the derived class a friend (as in the first example) turns that mistake into a compile error.

Usage notes

  • Cost model: calls through the base are ordinary function calls the optimizer can inline. There is no vtable pointer in the object, so CRTP classes can be smaller than virtual ones.
  • No common base: Shape<Square> and Shape<Rect> are unrelated types. You cannot put them in one std::vector; use virtual functions or std::variant when you need a heterogeneous container.
  • Incomplete type trap: inside the body of Base<Derived> you cannot use typename Derived::value_type for a member declaration, because Derived is not complete yet. Pass such types as extra template parameters or use them only inside member functions.
  • Mixin counters: static inline data in a CRTP base gives one counter per derived class, because each Counted<T> is a separate class.
  • Several mixins: a class can inherit from many CRTP bases at once, as Version does.
  • Modern alternative: since C++23 an explicit object parameter (this auto&& self) lets a plain base or the class itself call derived members without a template parameter in the base. It removes much of the need for CRTP.
  • Do not combine carelessly with slicing: copying a Base<D> by value from a D slices it. Make bases non-copyable on their own or protect the constructors.