In C++, a Curiously Recurring Template Pattern is an idiomatic approach to achieving Static Polymorphism, where a class derives from a class Template instantiation using itself as the template argument.
template <typename Derived>
struct Shape {
double area() const {
return static_cast<const Derived*>(this)->area_impl();
}
};
struct Circle : Shape<Circle> {
double radius;
double area_impl() const { return 3.14159 * radius * radius; }
};
struct Square : Shape<Square> {
double side;
double area_impl() const { return side * side; }
};