Comment by echoangle

7 hours ago

Couldn’t you just define that function arguments are evaluated left to right?

Or just throw an error.

Why? Just for this edge case? It could be faster and/or allow smaller code size to allow this to be undefined.

Undefined is also different from "depends on the compiler", because which behavior is chosen can even depend on the circumstances, whatever code appears before and/or after it.

That said, UB in code, such as this example of ordering of reads of volatile parameters being undefined, does not automatically mean that code that uses it is bad. It may very well be that the function being called doesn't misbehave either way.

  • That’s the point of the whole article. It’s not worth the speed gain to have a language that nobody can safely use because you can’t really prevent UB when you write it.

    > It may very well be that the function being called doesn't misbehave either way.

    The function being good or bad has nothing to do with the UB. The UB occurs before the function is called.

I meant reading the uninitialized variable

  • There is no uninitialized variable, I explicitly initialized it to 5.

    And yes indeed, C could do what Rust does and define the order of evaluation for function arguments.

    If the argument expressions are indeed side-effect-less, the compiler can always make use of the "as-if" rule and legally reorder the computation anyway, for example to alleviate register pressure.