r/cpp • • Mar 27 '26

C++26: A User-Friendly assert() macro

https://www.sandordargo.com/blog/2026/03/25/cpp26-user-friendly-assert
53 Upvotes

27 comments sorted by

View all comments

Show parent comment

3

u/EC36339 Mar 28 '26

That point of view assumes that all programmers will only ever use assert() to check for those conditions which, if not true, will definitely lead to UB

That's exactly what assertions are for. Nothing more or less. If assertions fail in your code at runtime, then your code is broken.

1

u/Fabulous-Meaning-966 Mar 28 '26

No, UB has a very specific meaning defined by the C++ standard. It does not just mean "the assumptions of your code are violated", it means "the compiler is allowed to do literally anything here". The latter is much worse to debug.

1

u/EC36339 Mar 28 '26

Fair enough.

But violating the assumptions of your code can cause UB.

And the distinction is more academic than practical, because a violation of the assumptions of your code may have worse consequences than an actual UB.

(Actual UB does in most cases lead to a crash. Robust architectures can handle crashes by having watchdogs that restart processes. UB is difficult to exploit by attackers, because it is ... well ... undefined. A deterministic, reproducible contract violation in the code, however, can have effects that have no mitigation and may enable repeatable and discoverable exploits)

1

u/Wooden-Engineer-8098 Mar 29 '26

It's trivial to debug, since in debug build assert will fire

1

u/Fabulous-Meaning-966 Mar 30 '26

UB normally triggers aggressive optimizations only in release builds. I was referring to debugging production failures. It is naive to think that all assertion violations will repro in debug builds.

2

u/Wooden-Engineer-8098 Mar 31 '26

How would you debug release build? You can start with enabling assertions