r/cpp_questions • • 3d ago

OPEN Where does the Curiously Recurring Template Pattern actually earn its keep in production code

I understand CRTP 👀 class Derived : public Base<Derived>, static polymorphism, no vtable overhead. Every tutorial shows the same toy example with `Shape` and `area()`, and then it stops. What I can't find is real-world usage. I'd love to hear where you've actually shipped it, and why prefer beat the Mixin or policy‑based design. Use cases I've seen so far:

Mixins, like std::enable_shared_from_this, to inject behavior into the derived class

Static interfaces for hot paths where virtual dispatch costs too much

Expression templates in Eigen

I'm still unsure about whether-

  1. Is it worth the readability cost since C++20 has concepts and deducing this?
  2. Any production pitfalls like error messages, code bloat, or slicing issues?

If you've used it in a codebase you maintain, I'd love to hear what problem it solved and if you derived any reusable patterns that you continue to use as snippet when shipping production code under limited time constraints. 🙏

6 Upvotes

15 comments sorted by

13

u/aruisdante 3d ago edited 3d ago

static polymorphism, no vtable overhead

I mean, that's it. That's the reason you use it. Deducing-this can replace some, but not all, cases that people would use CRTP (but you can also emulate deducing this without CRTP, they're kind of orthogonal really). Beyond performance considerations, there are other limitations of virtual:

  • It can't be used in constexpr until C++20, CRTP can.
  • It can't be used to inject a templated method, CRTP can.
  • It can't be used to inject hidden-friend operators (comparison or otherwise), CRTP can.

You also can't express an injectable API that needs to retain the concrete type information of the derived class in the return from the API in any other way than via CRTP.

You can see a bunch of real-world examples of CRTP use in arene-base, Woven by Toyota's C++ support library, of which I'm a co-author. Foonathan's type-safe also has a lot of examples.

Note that CRTP isn't an "either/or" with mixins and policy-based design. They're often used in complementary ways.

5

u/Ecstatic_Yak_ 3d ago

I've used it in multiple projects for interfaces on real time embedded target where vtables are unwanted. But since cpp23 I've been using concept which are a lot nicer and easier to work with. Its been used only for defining interfaces for quicker development and sharing between similar projects.

1

u/UnicycleBloke 1d ago

Can you say more about how you used CRTP? I currently use abstract interfaces for, for example, peripheral drivers. It helps with mocking or multiple implementations by passing a reference to the interface rather than the concrete type. It works very well but I don't need runtime polymorphism. So I looked at CRTP. It seemed like all the dependent types would end up becoming templates too. I balked at this viral template-ness. I suspect I missed something.

1

u/Ecstatic_Yak_ 1d ago

It seems I've used it the same way you did. And yes your whole codebase will become a templates "mess" which is inevitable and kind of the point. If your codebase is c++ 20 or more recent, don't even bother and switch to concepts

4

u/elperroborrachotoo 3d ago

ATL and WTL use it heavily to save on vtable size and enable code culling though inlining. The motivating factor was: website-embedded ActiveX controls dowloaded through a 1996 internet line. Every byte matters!

I've written quite some code in my life so it matches ATL style and user expectations. In the early years, the performance aspect was a nagging voice in the back but never really mattered in practice.

I think I've used it outside of that once or twice, but I can't put down down what it was.

5

u/YouFeedTheFish 3d ago

You don't need to do that any more. You can use deducing this in c++23.

https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/

4

u/Eric848448 3d ago

That syntax breaks my brain a little.

2

u/thefeedling 2d ago

The ergonomics brought by deducing this and concepts are underrated. Amazing features.

2

u/alfps 3d ago

"Deducing this" and the spaceship operator just about covers what CRTP was used for.

Now it would be nice if someone wrote a generally usable arithmetic operators mixin.

2

u/Discordbothost 3d ago

Each derived type can get its own copy of a mixin method. If code size matters, move the work that doesn't depend on Derived into a shared non-template function, then compare .text sizes with the same release flags.

1

u/tandycake 3d ago

1

u/conundorum 1d ago

That's a good use case for it: Injecting a shared interface that's consistent with the rest of the language.

1

u/PhotographFront4673 2d ago edited 2d ago

I once used it for geometric code with a relatively complex class hierarchy and templated dimensions. So the base class for a square knows how to keep tract of and reference it's edges, even if the edges contain data depending on the details defined by the concrete class of the square.

That said, I'm not sure I'd do it that way today. I've come to believe that inheriting functionality is rarely the best answer, and would reach towards composition and traits to reach a similarly functional result. Yes, concepts do make this style easier to organize and self document, but don't really change what is possible.

So I wouldn't criticize the CRTP so much as the OOP style that makes it occasionally useful. But if you are a practitioner of that style, and also have a problem where dynamic dispatch isn't good enough (performance, varying types, compile time constants) CRTP is occasionally invaluable.

0

u/ir_dan 3d ago

Deducing this is C++23. I used CRTP in its place for some method chaining fanciness.