Curiously Recurring Template Pattern (CRTP)

Learn about the curiously recurring template pattern commonly known as CRTP.

We'll cover the following

Overview

In the previous lesson, we learned how to implement dynamic polymorphism. But as we have seen, we have used virtual for the function in our base class. Using the virtual method, we are solving polymorphism at runtime. But what if we could solve the dynamic polymorphism in compile time without using virtual? This where we will use the curiously recurring template pattern (CRTP).

CRTP

CRTP is a C++ idiom used in early template programming developed by James Coplien. In C++, it is a powerful technique and a static alternative to virtual functions. A class is derived from a class template with itself as a parameter.

This is what it looks like in the code:

Get hands-on with 1200+ tech skills courses.