r/cpp_questions • u/iamrustocaml • 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-
- Is it worth the readability cost since C++20 has concepts and deducing this?
- 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. 🙏
13
u/aruisdante 3d ago edited 3d ago
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:constexpruntil C++20, 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.
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/
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
There's also another CRTP in std:
https://en.cppreference.com/cpp/ranges/view_interface
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.