Learn C++26 reflection hands-on with Lieven De Cock
Lieven, lead developer of Code::Blocks, takes C++26 reflection from the basics to working code, from padding detection to generating types at compile time.
📅 Thu 10 Dec, 11:00 AM ET, online | 30% OFF
✍️ From the editor’s desk
Welcome to the 75th issue of Deep Engineering!
On December 2, the Office of the National Cyber Director, the NSA, and CISA issued a joint statement on AI discoveries that affect some of the cryptography in use today. The agencies see no immediate, widespread threat to that cryptography, but they want government and industry to move faster on infrastructure upgrades.
For anyone updating a C++ library, even a small API change can create a bug that easily slips past review. Say a library function starts returning a value and your wrapper still returns a reference. The wrapper can compile even though the temporary object disappears before the caller can use it. C++26 makes that return a compile error.
Sándor Dargó, senior software engineer at Spotify, works on a production codebase that still targets C++20. Today’s issue features an excerpt from his practical deep dive on C++26, adapted from his live live session. He starts with that dangling reference, then shows how teams can already use a hardened standard library to catch code that reads past the end of a vector at runtime.
Let’s get started.
🔥 Upcoming Deep Engineering events
Join engineers, architects and technical leaders for live sessions on the systems and practices shaping modern software engineering.
📅 Sat 5 Dec: From One GPU to Thousands: Engineering Distributed AI Systems - Free
📅 Thu 10 Dec: C++26 Reflection in Practice
📅 Wed 20 Jan: Beyond main.cpp: Building a Maintainable C++ Project
📅 Sat 23 Jan: Low-Latency C++ for High-Frequency Trading (HFT)
🧠 Expert Insight
The Real Story of C++26 with Sándor Dargó
From a Deep Engineering live session with Sándor Dargó.
Watch Sándor’s full session and download his slides from the deep dive.
A bug that stops existing
Let’s start with this small function.
const int& f() {
return 42;
}This is a real, old bug. You return a reference to a temporary, and that dangling reference is a crash waiting to happen somewhere. Before C++26, this compiles. Well, it compiles with a warning if you’re lucky and you use good compiler settings, and not everyone does. Now it’s not a question of compiler settings anymore. C++26 makes sure this doesn’t compile.
No reflection. No contracts. Just a bug that stops existing.
In my opinion, that is the real story of C++26, and that’s what this piece is about: the smaller, sometimes not so small, changes that shape how we see C++.
I’m not going to get into the difficult moments C++26 went through over the last few weeks. But based on the latest news, for example on Herb Sutter’s blog, we can be optimistic that there is going to be something we call C++26. Whenever it’s officially standardized, these features are worth talking about.
Editor’s note: at the November 2026 meeting in Búzios, the committee completed the remaining technical work on C++26 and kept contracts in. The draft now goes to a second international ballot. Herb Sutter’s trip report has the details.
What you’ve heard about C++26
What you’ve heard about C++26 so far is probably one of three features.
Reflection:
std::meta, splicing, compile-time introspection.Contracts: preconditions, postconditions, assertions.
Senders/receivers: structured async execution.
They’re real and transformative, and they’re covered in dozens of other talks, so I won’t cover them here. If you want to go deeper on reflection, Lieven De Cock is running a hands-on C++ Reflection in Practice workshop with Deep Engineering on 10 December.
The big features change what’s possible. The small ones change what’s practical. I’m going to focus on the small ones, grouped into five themes:
Compile-time expansion:
constexprkeeps growing.Clearer correctness: redefining “wrong code.”
Safety via library: hardening the standard library.
Everyday expressiveness: fixing long-standing pain points.
Freestanding: C++ in more environments.
The fourth part is the longest. It covers all those small changes that fix the pain points we deal with while coding, or while reviewing our agents’ code.
I. Compile-time expansion
I have to admit this is the part I’m most afraid of, because I have to say constexpr so many times, and it’s hard to pronounce. But let’s look at how it has evolved over the last fifteen years or so.
It was introduced in C++11, and at first you couldn’t really do anything in a constexpr function. You could return literal types, and that was it. C++14 gave us loops and local variables, so it was already more useful. C++17 brought the compile-time if, so a function could go in different directions depending on whether we’re dealing with a constant expression or not, and we could create constexpr lambdas as well.
C++20 gave us even more: allocations at compile time, and with that, std::string and std::vector in constexpr code. They can’t survive compile time; they can’t cross into the runtime part of C++. But within our constexpr functions, we could already use them for calculations and logic. C++23 added more containers and static_assert improvements. And with C++26 we’re getting exception handling at compile time, placement new, virtual inheritance, structured bindings, and much more of the library.
Before C++26: exceptions stop constant evaluation
A constexpr function could already contain a throw. Take this simple factorial:
constexpr int factorial(int n) {
if (n < 0) throw std::invalid_argument(”don’t be negative”);
int result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
static_assert(factorial(5) == 120); // already OK before C++26
// constexpr int bad = factorial(-1);
// error: expression ‘<throw-expression>’ is not a constant expressionYou could evaluate this at compile time before C++26, as long as the function didn’t throw. Call it with a negative number at compile time and you got a compilation error saying the throw expression is not a constant expression. It didn’t work even if a surrounding catch would have handled it.
C++26: throw and catch at compile time
This is changing with C++26, because you can use both throw and catch at compile time. With the same factorial:
int x = factorial(-1); // runtime: throws
constexpr int y = factorial(-1); // compile-time: uncaught exceptionAt runtime, the throw is just a normal exception, and nothing changes there. At compile time, if the exception isn’t caught, you get a compilation error with the message you defined:
error: uncaught exception of type
‘std::invalid_argument’; ‘what()’: ‘don’t be negative’We all know C++ isn’t famous for friendly compiler errors. That has changed a lot and is still improving, and this is one of the examples where you can see it. And if a catch handles the exception during constant evaluation, evaluation simply continues, something that didn’t work before.
What else became constexpr in C++26?
On the language side:
Placement new: more flexible initialization at compile time.
Casting from
void*: this enables furtherconstexprwork instd::format,std::functionandstd::any.Structured bindings: they received quite a few improvements, which we’ll look at later, but interestingly you couldn’t declare them
constexpr. Now you can.
On the library side, I can’t even list everything that became constexpr:
<cmath>and<complex>: floating-point math at compile time.std::stable_sort,std::stable_partitionandstd::inplace_merge.All the standard containers, not just
vectorandstringanymore.std::constant_wrapper: pass compile-time constants as regular function arguments.
If you want to go deeper into this topic, I highly recommend Hana Dusíková’s recent talks.
To recap: now that we can use both throw and catch in constexpr code, we don’t need separate error handling for runtime and compile-time code, at least in more cases than before. Metaprogramming gets simpler, because you can use the patterns you’re used to instead of strange, hard-to-read tricks. Handled exceptions let compile-time evaluation recover and continue. And the distinction between runtime C++ and compile-time C++ is slowly disappearing. The mental model is just “write C++.”
II. Clearer correctness
C++26 redefines the correctness model. But first, why is that necessary?
The problem with undefined behavior
Until today, many programming errors simply resulted in undefined behavior. The compiler can assume undefined behavior doesn’t happen, and because of that, anything can go wrong. As we like to say, it could even delete everything on your hard drive. Usually that’s not the case, luckily, but nasal demons can appear, and compilers have no obligation to diagnose undefined behavior.
The C++ correctness model gave us no middle ground between correct code and “all bets are off.” So what does it even mean to write safe code? That’s a difficult question, but Dave Abrahams and Sean Parent gave this answer:
“A safe operation is one that cannot cause undefined behaviour.”
Dave Abrahams and Sean Parent
By that definition, it’s way too easy to write unsafe C++, because so many common mistakes are just undefined behavior. An off-by-one access past the end of a container is enough. Of course, if you write good, correct C++, you won’t hit most of these errors. But they’re there, because we don’t write good code all the time.
C++26 offers a few different answers.
The simplest fix: make it ill-formed
Some undefined behavior in C++26 simply becomes a compile error. We already saw the first example at the start.
// P2748R5: returned reference bound to temporary
const int& f() {
return 42; // C++23: UB — C++26: ill-formed
}
// P3144R2: deleting incomplete types
struct Widget; // forward declaration only
void cleanup(Widget* p) {
delete p; // C++23: UB — C++26: ill-formed
}Deleting an incomplete type was undefined behavior up to and including C++23. In C++26 it’s ill-formed. The compiler just rejects code it should probably have rejected a long time ago.
Compiler support: GCC 15 · Clang 19 (returned reference already enforced in GCC 14)
Introducing: erroneous behavior
That’s not the only answer C++26 gives…
Continue reading → Erroneous behavior, library hardening at 0.30% overhead, std::function_ref, std::indirect, inplace_vector and more, with all of Sándor's code.
🛠️ Tool of the Week
Cppcheck can catch lifetime and bounds errors in the C++20 code you ship today. The ArduPilot team runs it on pushes and pull requests for its Gazebo plugin.
Flag reads from uninitialized variables and struct members.
Spot functions that return references to temporary objects.
Catch array and container accesses that go past the end.
Use
compile_commands.jsonto check code with your project’s build settings.
📎 Tech Briefs
Microsoft tests how inconsistent documentation affects coding agents - Outdated docs raised token use by 38% and sometimes broke CI..
Google expands agent controls across Workspace and Cloud - Workspace MCP servers and CLI are now GA, with human approval for sensitive actions.
Anthropic tests Claude Code on logic bugs - Claude Code rediscovered 11 of 21 known bugs in a blind test.
AWS introduces scoped credentials for AI agents - Subagents get narrower temporary credentials under the caller’s IAM identity.
Researchers benchmark stack switching in WebAssembly - WasmFX beat Asyncify as C call stacks grew deeper.
That’s all for today. Thank you for reading Deep Engineering. We’ll be back next week with more expert-led insights.
Keep building,
Saqib Jan, Editor-in-Chief, Deep Engineering
Sponsor Deep Engineering
If your company wants to reach senior developers, software engineers, and technical decision-makers, speak to us about partnering with Deep Engineering.







On the C++20 codebases teams still ship, you can get both of these early: GCC already warns on them by default, so -Werror=return-local-addr -Werror=delete-incomplete turns "compiles with a warning if you're lucky" into a hard error today. Clang has the same pair (-Wreturn-stack-address, -Wdelete-incomplete).