Hard
CRTP base with a non-virtual default hook
Base provides a non-virtual step() as a "default" fallback, intended to be hidden by a derived class's own step(). Sloppy forgets to implement step(). What happens?
main.cpp
template <typename Derived>
struct Base {
void run() {
static_cast<Derived*>(this)->step();
}
void step() {
// "default" fallback behavior
static_cast<Derived*>(this)->run();
}
};
struct Sloppy : Base<Sloppy> {
// forgot to define step()
};
int main() {
Sloppy s;
s.run();
}8 of 44 in Design Patterns