3 ms·
You're right about it being up to the compiler if the conditions don't have side-effects. The C++ standard's "as-if" rule[1] allows these kinds of transformati
by mnurzia 4y ago
You're right about it being up to the compiler if the conditions don't have side-effects.
The C++ standard's "as-if" rule[1] allows these kinds of transformations if they do not effect the "observable behavior" of the program. Interestingly, if the values are marked volatile the compiler outputs the "correct" short-circuiting code, even with optimizations[2]
[1] https://en.cppreference.com/w/cpp/language/as_if https://en.cppreference.com/w/cpp/language/as_if
[2] https://cpp.godbolt.org/z/dzTh1vPW9 https://cpp.godbolt.org/z/dzTh1vPW9