GCC bug? Chaining methods, broken sequence point

后端 未结 3 2161
春和景丽
春和景丽 2021-02-19 03:26

I\'ve been debugging a program for some time, and eventually found the error was due to a reference not being updated as I thought it would be.

Here\'s a example that sh

3条回答
  •  不思量自难忘°
    2021-02-19 04:03

    Let me try interpreting the C++11 standard on this. In §1.9/15 it says:

    Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [...] If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

    Certainly int is an scalar type, and t.set(i).print(i + 5); contains a side effect on i in set() and the value computation i + 5, so if not noted otherwise, the behavior is indeed undefined. Reading §5.2.5 ("Class member access"), I could not find any notes about sequences regarding the . operator. [But see edit below!]

    Note, however that it is of course guaranteed that set() is executed before print() because the latter receives the return value of the former as an (implicit this) argument. The culprit here is that the value computation for print's arguments is unsequenced indeterminately sequenced relative to the call of set.

    EDIT: After reading the answer in your (@Xeno's) comment, I reread the paragraph in the standard, and in fact it later says:

    Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

    Because indeterminately sequenced is not unsequenced ("execution of unsequenced evaluations can overlap", §1.9/13), this is indeed not undefined behavior, but "just" unspecified behavior, meaning that both 15 and 5 are correct outputs.

    So when < means "sequenced before" and ~ means "indeterminately sequenced", we have:

    (value computations for print()'s arguments ~ execution of set()) < execution of print()

提交回复
热议问题