GCC Bugzilla – Bug 32133
Post-increment gained unintuitive behaviour
Last modified: 2007-05-29 10:08:27 UTC
I noticed a change in the implementation of the post-increment (i++) operator in gcc 4.0.1 (Apple build 5367).
The behaviour can be seen with the following code:
i += i++;
In older versions (< 4) of g++ the 'original' variable is returned by ++, meaning that the statement given has the effect of doubling the value of i. The new version of ++ seems to return the 'copy', leading to i just being incremented by one in the end.
The situation I think of as the most intuitive would be something like this:
(i += i)++
I realise though that operator precedence prohibits such behaviour.
The following two lines demonstrate, however, why I think the old behaviour is desired over the new one:
(i++ < i); //Used to be true, now false
(i > i++); //Idem
Modern versions even make this true:
i++ == i++
Which, I think, is not what one wants.
You obviously did not read the bugs.html page which lists this as a nonbug:
Increment/decrement operator (++/--) not working as expected - a problem with many variations.
The following expressions have unpredictable results:
i*(++i) /* special case with foo=="operator*" */
std::cout << i << ++i /* foo(foo(std::cout,i),++i) */
since the i without increment can be evaluated before or after ++i.
The C and C++ standards have the notion of "sequence points". Everything that happens between two sequence points happens in an unspecified order, but it has to happen after the first and before the second sequence point. The end of a statement and a function call are examples for sequence points, whereas assignments and the comma between function arguments are not.
Modifying a value twice between two sequence points as shown in the following examples is even worse:
(++i)*(++i) /* special case with foo=="operator*" */
std::cout << ++i << ++i /* foo(foo(std::cout,++i),++i) */
This leads to undefined behavior (i.e. the compiler can do anything).
*** This bug has been marked as a duplicate of 11751 ***