Title
Assignment evaluation order
Status
nad
Section
7.6.19 [expr.ass]
Submitter
Jason Merrill

Created on 2016-07-06.00:00:00 last changed 81 months ago

Messages

Date: 2017-04-15.00:00:00

Rationale (April, 2017):

Class copy assignment binds a const T&, so the A example actually yields a.i == 1 after the assignment.

Date: 2022-11-20.07:54:16

P0145 caused this situation:

  extern "C" void abort();

  struct A
  {
    int i;
    int data[10000];
  } a;

  A& aref() { a.i++; return a; }
  int main()
  {
    aref() = a;
    if (a.i != 0)
      abort();
  }

Is a.i now required to be 0?

A related example is this:

  int b;
  int& bref() { ++b; return b; }
  int main() {
    bref() = b;
    if (b != 0)
      abort();
  }

Here, b is required to be 0 after the assignment, because the value computation of the RHS of the assignment is sequenced before any side-effects on the LHS. The difference in guaranteed behavior between class and non-class types is disturbing.

History
Date User Action Args
2018-02-27 00:00:00adminsetmessages: + msg6010
2016-07-06 00:00:00admincreate