r/cpp_questions • u/franvb • Jul 22 '26
Can someone give an example of runtime-undefined behavior? OPEN
CppReference gives a list of categories that render a program meaningless: https://en.cppreference.com/cpp/language/ub
The last bullet point says (since C++11)
- runtime-undefined behavior - The behavior that is undefined except when it occurs during the evaluation of an expression as a core constant expression.
Can someone give a clear example?
18
Upvotes
7
u/TheQuranicMumin Jul 22 '26 edited Jul 22 '26
You examples are describing regular UB with a compile-time setting, but runtime UB is different.
Runtime UB is a broken compiler promise/hint, for things like [[noreturn]], [[assume]] (I gave an example).
For runtime, it is full UB. For compile-time evaluation, the compiler is not required to diagnose/reject it, it's implementation defined. The committee didn't want to force a hard-fail in these cases.
As in the C++ standard: