0
0
CppConceptIntermediate · 3 min read

What is CRTP in C++: Curiously Recurring Template Pattern Explained

The CRTP (Curiously Recurring Template Pattern) in C++ is a technique where a class template uses its derived class as a template parameter. This allows static polymorphism, enabling compile-time method calls without the overhead of virtual functions.
⚙️

How It Works

Imagine you have a base class that wants to use features of a class that inherits from it, but you want to decide exactly which class at compile time. CRTP lets you do this by making the base class a template that takes the derived class as a parameter. This way, the base class can call methods of the derived class directly, without waiting for runtime decisions.

Think of it like a recipe book (base class) that is customized for a specific chef (derived class). The recipe book knows the chef's style because it is written specifically for that chef. This lets the recipe book call the chef's special techniques directly, making the cooking faster and more efficient.

In C++, this means the compiler can optimize calls and inline functions, avoiding the cost of virtual functions and enabling more flexible code reuse.

💻

Example

This example shows a base class template calling a method from its derived class using CRTP.

cpp
#include <iostream>

// Base class template takes Derived as a parameter
template <typename Derived>
class Base {
public:
    void interface() {
        // Calls a method implemented in Derived
        static_cast<Derived&>(*this).implementation();
    }

    void common() {
        std::cout << "Common behavior in Base\n";
    }
};

// Derived class inherits from Base, passing itself as template argument
class Derived : public Base<Derived> {
public:
    void implementation() {
        std::cout << "Derived implementation called\n";
    }
};

int main() {
    Derived d;
    d.interface();  // Calls Derived::implementation via Base
    d.common();     // Calls Base::common
    return 0;
}
Output
Derived implementation called Common behavior in Base
🎯

When to Use

Use CRTP when you want to achieve polymorphism without the cost of virtual functions. It is useful in performance-critical code where decisions can be made at compile time.

Common use cases include:

  • Static polymorphism in libraries or frameworks
  • Implementing mixins or reusable components
  • Enabling compile-time interface checks
  • Optimizing code by inlining derived class methods

CRTP is often seen in template libraries like the C++ Standard Library and Boost, where flexibility and speed are important.

Key Points

  • CRTP uses a class template that takes the derived class as a parameter.
  • It enables static polymorphism, avoiding runtime overhead.
  • Derived class methods can be called from the base class using static_cast.
  • It is useful for performance-sensitive and template-based designs.

Key Takeaways

CRTP enables compile-time polymorphism by passing the derived class as a template parameter to the base class.
It avoids virtual function overhead by allowing direct calls to derived class methods.
Use CRTP for performance-critical code and reusable template-based designs.
The base class uses static_cast to access derived class implementations.
CRTP is common in advanced C++ libraries for flexible and efficient code.