Almost any question on undefined behavior elicits an answer saying that "undefined behavior may time travel" and/or that "the presence of undefined behavior makes the entire program undefined".
In the simplified example from this question, UB is said to enable the compiler to eliminate the else
branch altogether, optimizing
void foo(int* p)
{
if ( p ) {
bar();
} else {
baz();
*p = 1;
}
}
to
void foo(int* p)
{
bar();
}
But what if baz()
contains an observable operation (I/O) or throws an exception?
Even though the C++ Standard places no requirement on the implementation executing a program which would result in undefined behavior (not even with regard to operations preceding the first undefined operation), a standard-compliant non-malicious compiler should not take advantage of that provision by injecting artificial calls to nasal demons as soon as it detects a possibility of UB. The most that the compiler can do about UB is to assume that it doesn't happen and optimize accordingly.
If we only consider such compilers, what are the barriers that should stop the propagation of UB?