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:
- Static polymorphism
- Template metaprogramming techniques