What is the Curiously Recurring Template Pattern (CRTP) and what is it used for?
Last updated
CRTP (Curiously Recurring Template Pattern): a class derives from a template base instantiated with itself,
class Derived : public Base<Derived>, so the base canstatic_castto the derived type at compile time.Used for static polymorphism (no vtable lookup, allows inlining) and mixins that inject behavior, like
enable_shared_from_this, comparison operators, or instance counting.Trade-off: there is no common base type, so different derived types can't share one homogeneous container as with virtual functions.
- https://www.interviewbit.com/blog/web-stories/10-hidden-cpp-features-that-will-impress-interviewers/
- https://github.com/downdemo
- https://github.com/25-masik-52/CppInterviewKit
- https://utkrusht.ai/blog/cplusplus-interview-questions
- https://dev.to/sandordargo/the-curiously-recurring-template-pattern-crtp-46j7
- https://github.com/satyajeetramnit/oops-for-humans