Curiously Recurring Template Pattern

The curiously recurring template pattern (CRTP) is an idiom in C++ in which a class X derives from a class template instantiation using X itself as template argument. It is a form of F-bounded Quantification.

// The Curiously Recurring Template Pattern (CRTP)
template <class T>
class Base
{
    // methods within Base can use template to access members of Derived
};

class Derived : public Base<Derived>
{
    // ...
};

Some use cases for this pattern are:

  1. Static polymorphism
  2. Template metaprogramming techniques

Subscribe to अमन Mehara

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe