void foo() {
int a[5], c = 2;
for (int i = 0; i < 5; i++)
a[i] = 0;
int res = (c--, a[c]++, c++) + (c++, a[c]--, c--);
for (int i = 0; i < 5; i++)
cout << i << ": " << a[i] << endl;
}
The code above will print:
0 : 0
1 : 1
2 : -1
3 : 0
4 : 0
Instead of:
0 : 0
1 : 1
2 : 0
3 : -1
4 : 0
This is so because the operations order in generated code is the following:
// first parentheses
c--;
a[c]++;
// second parentheses
c++;
a[c]--;
// and only then the last operation
res = c++ + c--;
The question is: why operations do not run as expected (i.e. all three operations in one parentheses and then all three operations in other)?