← Back to context

Comment by shakna

8 hours ago

Only if there would be no side-effects. Which there are.

No this is irrelevant for making this decision

  • I've mentioned elsewhere the standards, and compilers as well, disagreeing with you here.

    But feel free to run against the various compilers through godbolt. [0] They won't optimise the branch away. Access to a volatile, must be preserved, in the order that they exist. No optimisation, UB or otherwise, is allowed to impede that. Because an access is a side-effect.

    [0] https://godbolt.org/z/85cGhq3Ta

This looks like a long back and fourth, that can easily be solved by a minute or two on godbolt...

  • > that can easily be solved by a minute or two on godbolt...

    Unfortunately it's not that simple when it comes to UB. If the snippet in question does in fact exhibit UB then there's no guarantee whatever Godbolt shows will generalize to other programs/versions/compilers/environments/etc.

    • That's very funny to me.

      A) x is always removed.

      B) no, it's never removed if volatile.

      But neither person can prove what a compiler will actually do, despite claiming they'll always act a certain way given 5 lines of code.

      4 replies →