The Curiously Recurring Template Pattern (CRTP) is a heavily used idiom in C++. It is similarly resistant to understanding as the classic design pattern visitor I presented in my last post: “C++23: Deducing This“. Thanks to deducing this, we can remove the C and R from the abbreviation.
C++23: Syntactic Sugar with Deducing This
By Rainer Grimm
From the article:
CRTP
The acronym CRTP stands for the C++ idiom Curiously Recurring Template Pattern and denotes a technique in C++ in which a class
Derived
is derived from a class templateBase
. The critical point is thatBase
hasDerived
as a template argument.template <typename T> class Base{ ... }; class Derived : public Base<Derived>{ ... };CRTP is typically used to implement static polymorphism. Unlike dynamic polymorphism, static polymorphism happens at compile time and does not require an expensive run-time pointer indirection.
Add a Comment
Comments are closed.