werasm said:
Pete Becker wrote: ....
Ok, rephrase: How often does it lead to unwanted behaviour in practise,
given the specific example?
This is where the scientist and the engineer diverge.
There are many cases where it would not happen with the current
compilers and if in some wild future the compiler behaves differently,
in all likeliness the code would fail to run at all (SEGV and such) then
it's safe to use the compiler specific behaviour.
One case where it is not safe is things like:
int x = ..
int y = ..
return x << y; // if y is larger than the number of bits in an int it is UB
In this case some systems will return x and some will return 0. This
can lead to subtle errors that could one day trigger something very
undesirable giving little warning of the lurking failure mode.
On the other hand ...
char x[5];
int y = * reinterpret_cast< int * >( x + 1 );
will fail with a SIGBUS on systems that don't allow unaligned access.
You have plenty of warning in the form of the OS telling you to get
stuffed, in this case UB is well defined. Yes, theoretically, some new
platform can do something strange but in all likelihood that will not
happen for a number of reasons.
So the real response to Pete's question is :
"What is the current set of manifestations of UB for this example in
todays 4 most common compilers ?"
If the answer comes back, they all do the same thing, then you can
pretty much say it's a defacto standard and that any future compiler
will probably behave the same way.
At the then of the day, popular compilers have a very large intersection
in their problem space so it's safe to bet they more likely than not
will take the same UB design choices.