This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/31652] New: postfix increment semantics implemented incorrectly
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 21 Apr 2007 22:39:08 -0000
- Subject: [Bug c++/31652] New: postfix increment semantics implemented incorrectly
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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