public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/27344]  New: associativity of * operator incorrect when compiled without optimization
@ 2006-04-27 23:53 mike dot clarkson at spacedev dot com
  2006-04-28  0:00 ` [Bug c++/27344] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: mike dot clarkson at spacedev dot com @ 2006-04-27 23:53 UTC (permalink / raw)
  To: gcc-bugs

Here is the version and system info:

% g++ -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.5/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)


Here is the output from the test program (which is included below)
% g++ -o associativityTest -O3 associativityTest.cpp
% ./associativityTest
neg_50 = -50
% g++ -o associativityTest associativityTest.cpp
% ./associativityTest
neg_50 = 50

You can see the different answers above, as a result of compiling with and
without optimization. The answer of -50 is correct, based on the left to right
associativity of the * operator.

Here is the test file associativityTest.cpp:

#include <iostream>

using namespace std;

class Int
{
public:
  explicit Int(int value)
    : _value(value)
  {
  }

  Int neg()
  {
    _value *= -1;
    return *this;
  }

  int value()
  {
    return _value;
  }

  Int operator*(Int right)
  {
    return Int(_value*right._value);
  }

private:
  int _value;
};

Int operator*(Int I, int i)
{
  return I*Int(i);
}

Int operator*(int i, Int I)
{
  return I*i;
}

int main()
{
  Int five(5);

  Int neg_50 = (five * 2 * five.neg());

  cout << "neg_50 = " << neg_50.value() << endl;
}


-- 
           Summary: associativity of * operator incorrect when compiled
                    without optimization
           Product: gcc
           Version: 3.4.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mike dot clarkson at spacedev dot com


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


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

* [Bug c++/27344] associativity of * operator incorrect when compiled without optimization
  2006-04-27 23:53 [Bug c++/27344] New: associativity of * operator incorrect when compiled without optimization mike dot clarkson at spacedev dot com
@ 2006-04-28  0:00 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-04-28  0:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-04-28 00:00 -------
(five * 2 * five.neg());

This is equivlant to:
(five * 2) * (five.neg())
but the C and C++ standard do not specify which expression is evulated first.
In that (five * 2) might be evulated first or five.neg().  five.neg() has a
side effect of changing five (_value *= -1;) which makes the above statement
undefined as five is accessed and written to without a sequence point
inbetween.


And it makes it a duplicate of PR 11751.

*** This bug has been marked as a duplicate of 11751 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


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


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

end of thread, other threads:[~2006-04-28  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-27 23:53 [Bug c++/27344] New: associativity of * operator incorrect when compiled without optimization mike dot clarkson at spacedev dot com
2006-04-28  0:00 ` [Bug c++/27344] " pinskia 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).