This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


2286. Assignment evaluation order

Section: 7.6.19  [expr.ass]     Status: NAD     Submitter: Jason Merrill     Date: 2016-07-06

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.

Rationale (April, 2017):

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