public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/31652]  New: postfix increment semantics implemented incorrectly
Date: Sat, 21 Apr 2007 22:39:00 -0000	[thread overview]
Message-ID: <bug-31652-14435@http.gcc.gnu.org/bugzilla/> (raw)


g++ parses the code correctly and calls the correct overloaded
increment operators, but in the wrong postfix order.  The semantics of
postfix are to take the rvalue before invoking the method.  Note this
is not related to multiple reference ordering between sequence points
as the object is only referenced once.  Although I have not checked
postfix decrement, it may have the same semantic problem and it is
trivial to adapt the supplied code.

Environment:
System: Linux dbox 2.6.18-3-686 #1 SMP Sun Dec 10 19:37:06 UTC 2006 i686
GNU/Linux
Architecture: i686


host: i486-pc-linux-gnu
build: i486-pc-linux-gnu
target: i486-pc-linux-gnu
configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --with-tune=i686
--enable-checking=release i486-linux-gnu

How-To-Repeat:
cat > a.cpp
#include <iostream>

class C
{
public:
  int v;

  C( int v ) : v( v ) {}

  C & operator++( void ) { v++;      return *this; }
  C & operator++( int )  { v += 100; return *this; }
};

main()
{
  C a( 0 );
  C b( 0 );

  std::cout << "a:" << a.v << std::endl;  // 0
  std::cout << "b:" << b.v << std::endl;  // 0

  b = ++a;

  std::cout << "a:" << a.v << std::endl;  // 1
  std::cout << "b:" << b.v << std::endl;  // 1

  b = a++;

  std::cout << "a:" << a.v << std::endl;  // 101
  std::cout << "b:" << b.v << std::endl;  // should be 1, not 101!
}
^D
> g++ a.cpp ; ./a.out
a:0
b:0
a:1
b:1
a:101
b:101


------- Comment #1 from hayward at loup dot net  2007-04-21 23:39 -------
Fix:
Execute postfix methods *after* evaluating the expression they are in.


-- 
           Summary: postfix increment semantics implemented incorrectly
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hayward at loup dot net
 GCC build triplet: i486-pc-linux-gnu
  GCC host triplet: i486-pc-linux-gnu
GCC target triplet: i486-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31652


             reply	other threads:[~2007-04-21 22:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-21 22:39 gcc-bugzilla at gcc dot gnu dot org [this message]
2007-04-21 22:49 ` [Bug c++/31652] " pinskia at gcc dot gnu dot org
2007-04-22  5:40 ` hayward at loup dot net
2007-04-22  6:04 ` pinskia at gcc dot gnu dot org
2007-04-22 15:37 ` hayward at loup dot net
2007-04-22 16:50 ` drow at gcc dot gnu dot org
2007-04-22 17:18 ` pinskia at gcc dot gnu dot org
2007-04-22 17:49 ` drow at false dot org
2007-04-22 17:49 ` mmitchel at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-31652-14435@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).