Sequence point in assignment statements

Take, for example, the specific assignment operator ^= . Stack Overflow.site/questions/445508 / ... says that modifying the left operand may not have been performed after evaluating ^= and thus the code a ^= b ^= a ^= b undefined behaivor. But this does not seem to be the case. The standard says in 5.17 [expr.ass] that

In all cases, the assignment is ordered after calculating the value of the right and left operands and before calculating the value of the assignment expression.

There are two key points in this statement. 1) What does the subject task relate to? In my opinion, this applies only to the modification of the left operand. 2) What does the calculation of the values โ€‹โ€‹of an assignment expression mean? cppreference says that it refers to returning a reference to the changed object (my selection).

As a conclusion, the left operand should have been changed after evaluating ^= , which contradicts what (most) people think. Did I miss something?

+2
source share
2 answers

You refer to question C. However, this does not matter, since C and C ++ are different languages.

In addition, sequence points no longer exist with C11 and C ++ 11; instead, there are relationships sequenced to, unclassified and vaguely ordered.

In this quote:

  • assignment means writing to memory a .
  • computing the value of an expression means computing the value of this expression. (Example - a 2 + 2 value is 4 , and calculating a value is the process of determining that 4 was a value).

There are two calculations of the values: a ^ b and a = (this result).

In the cited text for a = a ^ b everything should happen in the following order:

  • Extract values โ€‹โ€‹from a and b (in any order) and determine the location of the memory where the result will be stored (calculation of the value of the right and left operand, respectively)

  • Save the result to a (assignment). It involves calculating the value a ^ b , which is not mentioned in the quote, but explicitly the result must be calculated before it is saved.

  • Calculate the value of the assignment expression. This means that the value stored in a is ready to use the surrounding expression (evaluating the value).

You are right that 2 and 3 seem to be โ€œbackwardโ€ compared to what you can do on paper. But remember that in general y differs from x = y . The value of the assignment expression matches the value stored in x . (Example: int x; double y = (x = 6.5); - then y is 6 , not 6.5 ). So we can do this by storing the result in a , and then suggesting a as the result.

+2
source

a ^ = b ^ = a ^ = b There are 3 left operands, without a clear rule which should be used first in computa p>

-2
source

Source: https://habr.com/ru/post/984350/


All Articles