@ telion: all write operators also do a read. The point I meant to say was that you can't do an
additional read with that write.
Terminux's latest example illustrates this beautifully:
1 2 3
|
int b[3] = { 1,2,3 };
int* a = b;
cout << *(a++) << " " << *a << " " << *(--a) << endl;
|
This output is perfectly legal. Here's what the compiler is doing:
1 2 3 4 5 6
|
--a; // pre-decrementing (decrement before the --a is printed)
cout << a; // the "a++" print, printing before "a++" is incremented
// prints b[-1] (garbage)
a++; // post-incrementing a (perform the a++ incrementing)
cout << a; // the "*a" print, printing b[0] (1)
cout << a; // the "--a" print, printing b[0] (1)
|
Notice that even though the output is not what you might have expected, the compiler legally obeyed all operator precedence rules. That is:
1) "--a" decremented a before it was read (decremented on line 1, read on line 6)
2) "a++" incremented a after it was read (read on line 2, incremented on line 4)
3) -- and ++ were both evaluated before their respective << operator
The language rules say
nothing about whether --a or a++ have to be evaluated first. Therefore the behavior is undefined.
You should never, ever do this in any program.