CRTP: A Curious Polymorphism Idiom

Polymorphism in C++

  • Definition: the use of a single symbol to represent multiple different types
  • Compile-time (static) polymorphism
    • Function overloading: implemented by name mangling, not available in C
    • Operator overloading: similar as function overloading
    • Templates
      • CRTP <- today we focus on
  • Runtime (dynamic) polymorphism
    • Function overriding (virtual) -> also can be inferred early at compile time / constexpr virtual (C++20)
    • std::optional (C++17)
    • std::variant (C++17)
    • ...
  • A takeaway here: look into if variables can be inferred during compile time
  • We prefer compile-time polymorphism in common, but still need to be aware of the overhead
    • Binary size
    • Codebase complexity

A Curious Idiom: CRTP

  • CRTP stands for Curiously Recurring Template Pattern
  • a class X derives from a class template instantiation using X itself as a template argument
    template <class T>
    class X { };
    class A : public X<A> {...};
  • Used for enhancement or semantic extension
  • No virtual (virtual is not bad fairly speaking)

Why can CRTP compile


When to use CRTP