As-if transformations maintain the actions of the written program under the assumption that undefined behaviour does not occur. The two are fundamentally intertwined.Here's a few thought experiments: [...]Today, all these optimizations are justified by the fact that any attempt to observe the difference before and after optimization must necessarily execute undefined behaviour, and executing undefined behaviour is a priori assumed to be impossible.It's possible that there's another way to show that these and other optimizations are valid, but I would need to see a proposal on how to do that. I've tried to create one myself and failed. Frankly, I'm starting to believe that our current situation with UB really is the best possible design choice.
Can you fix this without adding a listing of CPU instructions to the language standardand without fully defining it?
I don't need to. The C++ Standard already specifies that atomic integers obey the rules
of 2's-complement arithmetic, and it doesn't spell out what those rules are.I would consider it shall "obey the rules of 2's complement arithmetic" to be fully defined, as 1's complement machines would have to simulate 2's complement arithmetic. (Offhand, I think this is what happens with atomics today; 1's complement machines are expected to use a library call to implement the atomic and ensure 2's complement?)