Undefined behavior, or: Does Swift have sequence points?

前端 未结 1 882
天命终不由人
天命终不由人 2021-01-02 18:00

In C/C++, the second statement in

int i = 0;
int j = i++ + i++ + ++i;

invokes both

  • unspecified behavior, because the
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 18:18

    The question was answered by Apple developer and Swift designer Chris Lattner in the Apple Developer Forum https://forums.developer.apple.com/thread/20001#63783:

    Yes, the result of that expression will always be 4. Swift evaluates expressions left to right, it isn't undefined or implementation defined behavior like C.

    Chris also added:

    That said, if you write code like that, someone trying to maintain it will probably not be very happy with you

    Agreed! It was meant as an extreme example to demonstrate the problem.

    0 讨论(0)
提交回复
热议问题