3 ms·
>is since UB is not allowed, the compiler can elide checks that would protect against UB for the sake of optimization This is incorrect. If a check prevents UB
by ivanbakel 2y ago
>is since UB is not allowed, the compiler can elide checks that would protect against UB for the sake of optimization
This is incorrect. If a check prevents UB, then the compiler isn't allowed to optimise it away, because the resulting program would then contain UB when it didn't before. Part of the compiler contract is that compilers won't introduce UB - they can only rely on UB already present in the program.
Some people think that you can "catch" UB after the fact, but that's an incorrect understanding of how it works. Once UB has occurred, there is no way to go back to a coherent program state and code which would execute after some UB is therefore irrelevant.