User account creation filtered due to spam.

Bug 32133 - Post-increment gained unintuitive behaviour
Summary: Post-increment gained unintuitive behaviour
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-28 22:44 UTC by Jouke Witteveen
Modified: 2007-05-29 10:08 UTC (History)
53 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jouke Witteveen 2007-05-28 22:44:58 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.

Regards,
- Jouke
Comment 1 Andrew Pinski 2007-05-28 22:47:19 UTC
You obviously did not read the bugs.html page which lists this as a nonbug:
http://gcc.gnu.org/bugs.html#nonbugs_c

Increment/decrement operator (++/--) not working as expected - a problem with many variations.

    The following expressions have unpredictable results:

x[i]=++i
foo(i,++i)
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
foo(++i,++i)
(++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 ***