<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/74851>74851</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang][question] Order of evaluation of assignment operator with references on both sides
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nicovank
      </td>
    </tr>
</table>

<pre>
    Consider the following code:
```cpp
static int i = 71;
static int j = 13;

int& f() { return i; }

int& g() { return j; }

int main() {
    f() = g();
}
```

Four things need to happen:
 1. Evaluation of RHS (call `g`).
 2. Read from RHS reference.
 3. Evaluation of LHS (call `f`).
 4. Write read value to LHS reference.

Both Clang and GCC generate instructions matching this order. Now what if `f` had side effects on `j`?
Godbolt: https://godbolt.org/z/ssf7MTh74

If we were dealing with non-integral types, these side-effects would happen because of the indirection.
Godbolt: https://godbolt.org/z/6o7s9cK5h

Intuitively, it feels like reading from the reference should be done after evaluating the LHS (swap steps 2 and 3), so that the integral behavior matches complex type behavior, but this is not the case. Is this execution order clearly defined and correctly implemented, unspecified and chosen to happen this way, or undefined? Looking through the standard and [cppreference](https://en.cppreference.com/w/cpp/language/eval_order), I am still not sure.

Underlying context: maps without reference stability, where `operator[]` is used on both sides of an operator expression.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU-P47YP_TTKhRjDkZM4OeSwSX7e36DTFti26LGQJdrWjCK5ohxP-ukLyfkz2Z1LgcABTPrx8fFREkS6tYhbttyx5WEmhtA5v7VaupOwb7PaqfN27yxphR5Ch9A4Y9yobQvSKWTFF5YfWP6FrfLpJ_t-ekNBBC1B2wAaWHGAcs6K3Q-x1xSbF7fY9NQ2ML6ChvE14xtg5Q48hsFb0KzYASsPn2S3P2a_fp4NR6HtPXt6DwD3gsXhCndndoO5NvsRtXJDVEjblsAiKggOOtH3aG8iwTy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