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

前端 未结 11 1670
再見小時候
再見小時候 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 06:00

    C++17 defines stricter evaluation rules. In particular, it sequences function arguments (although in unspecified order).

    N5659 §4.6:15
    Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which. [ Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. —end note ]

    N5659 § 8.2.2:5
    The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

    It allows some cases which would be UB before:

    f(i = -1, i = -1); // value of i is -1
    f(i = -1, i = -2); // value of i is either -1 or -2, but not specified which one
    

提交回复
热议问题