public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/31652]  New: postfix increment semantics implemented incorrectly
@ 2007-04-21 22:39 gcc-bugzilla at gcc dot gnu dot org
  2007-04-21 22:49 ` [Bug c++/31652] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gcc-bugzilla at gcc dot gnu dot org @ 2007-04-21 22:39 UTC (permalink / raw)
  To: gcc-bugs


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-04-22 17:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-21 22:39 [Bug c++/31652] New: postfix increment semantics implemented incorrectly gcc-bugzilla at gcc dot gnu dot org
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

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).