>Submitter-Id: net >Originator: Mike Hayward >Organization: Loup Technologies, Inc. >Confidential: no >Synopsis: postfix increment semantics implemented incorrectly >Severity: serious >Priority: medium >Category: c++ >Class: wrong-code >Release: gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) >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 >Description: 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. >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 >Fix: Execute postfix methods *after* evaluating the expression they are in. -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]