CRTP: Curiously Recurring Template Pattern
CRTP happens when you pass a class as a template parameter to its base class:
template
struct BaseCRTP {};
struct Example : BaseCRTP {};
Within the base class, it can get ahold of the derived instance, complete with the derived type, simply by casting (either static_cast or dynamic_cast work):
template
struct BaseCRTP {
void call_foo() {
Derived& self = *static_cast(this);
self.foo();
}
};
struct Example : BaseCRTP {
void foo() { cout << "foo()\n"; }
};
In effect, call_foo has been injected into the derived class with full access to the derived class's members.
Feel free to edit and add specific examples of use, possibly to other SO posts.