Why is f(i = -1, i = -1) undefined behavior?

前端 未结 11 1673
再見小時候
再見小時候 2020-12-04 05:28

I was reading about order of evaluation violations, and they give an example that puzzles me.

1) If a side effect on a scalar object is un-sequenced r

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 05:58

    The fact that the result would be the same in most implementations in this case is incidental; the order of evaluation is still undefined. Consider f(i = -1, i = -2): here, order matters. The only reason it doesn't matter in your example is the accident that both values are -1.

    Given that the expression is specified as one with an undefined behaviour, a maliciously compliant compiler might display an inappropriate image when you evaluate f(i = -1, i = -1) and abort the execution - and still be considered completely correct. Luckily, no compilers I am aware of do so.

提交回复
热议问题