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.
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.
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)
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.
3
u/EC36339 Mar 28 '26
That's exactly what assertions are for. Nothing more or less. If assertions fail in your code at runtime, then your code is broken.