Consider the following piece of code:
int x = 1;
int y = 2;
y = x + (x = y);
When this runs in C#, the variables end up assigned with these values:
x = 2
y = 3
On the other hand, when the same runs in C++, the variables end like this:
x = 2
y = 4
Clearly, the C++ compiler is using different precedence, associativity and order of evaluation rules than C# (as explained in this Eric Lippert's article).
So, the question is:
Is it possible to rewrite the assignment statement to force C++ to evaluate the same as C# does?
The only restriction is to keep it as a one-liner. I know this can be rewritten by splitting the assignments into two separate lines, but the goal is to maintain it in a single line.
Yes, it is possible indeed.
x = (y+=x) - x;
So simple.