User account creation filtered due to spam.

Bug 2673

Summary: c++ calls by value instead of by reference
Product: gcc Reporter: mayer
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: critical CC: algorithmus, arminu, asokumar, av1474, bala, barnarr, bmead15, buergel, carpman, ceniza666, chuchunxin, devnull, d_picco, eric.mcvicker, gaurav_har, gcc-bugs, gcc, ggs, horsh, jandres, janis, jompo, krs, lid, lindahlb, lxg8906, mayer, mayer, mikaldaz, nakkore, pierre.van.de.laar_at_philips.com, pme, qyang, raoulgough, rglan, rjvbertin, robc, s9322036, smartmouse714, suan, super.aorta, svetozarmarkov, tczarnecki, vanveghel, zshao
Priority: P1 Keywords: wrong-code
Version: 3.0   
Target Milestone: ---   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed:

Description mayer 2001-04-27 20:36:00 UTC
When calling a function with a call-by-reference inside a printing
statement the variable does not get updated, effectively calling as
a call-by-value.

Release:
3.0 20010409 (prerelease)

Environment:
System: Linux tosca 2.2.18 #2 Thu Mar 8 13:48:25 PST 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc/configure --prefix=/usr/local/gcc --disable-nls

How-To-Repeat:
Compile and run this program:

#include <iostream>
using namespace std;
double f(double& x)
{
  x=1;
  return 2;
}
int main()
{
   double d=5;
   cout << f(d) << "\t" << d << endl;
   // the line below produces correct output of "2    1"
   // double y=f(d); cout << y << "\t" << d << endl;
}

On my system it outputs:
2	5
instead of the correct:
2	1
Comment 1 Phil Edwards 2001-04-28 21:31:00 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Thank you for your report.
    
    Note that "Priority: high" is reserved for GCC maintainers.
    
    This is not a bug.  There is no defined order of evaluation
    for a statement like
    
        cout << f(d) << "\t" << d << endl;
    
    so 'd' may be evaluated before 'f(d)'.
Comment 2 Phil Edwards 2001-04-29 01:31:00 UTC
From: pme@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org, mayer@tosca.localnet, nobody@gcc.gnu.org
Cc:  
Subject: Re: c++/2673
Date: 29 Apr 2001 01:31:00 -0000

 Synopsis: c++ calls by value instead of by reference
 
 State-Changed-From-To: open->closed
 State-Changed-By: pme
 State-Changed-When: Sat Apr 28 21:31:00 2001
 State-Changed-Why:
     Thank you for your report.
     
     Note that "Priority: high" is reserved for GCC maintainers.
     
     This is not a bug.  There is no defined order of evaluation
     for a statement like
     
         cout << f(d) << "\t" << d << endl;
     
     so 'd' may be evaluated before 'f(d)'.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2673&database=gcc

Comment 3 mayer 2001-04-29 11:42:42 UTC
From: "Uwe F. Mayer" <mayer@tux.org>
To: <gcc-gnats@gcc.gnu.org>
Cc: <pme@gcc.gnu.org>
Subject: Re: c++/2673
Date: Sun, 29 Apr 2001 11:42:42 -0400 (EDT)

 I am withdrawing my bug report with Synopsis: c++ calls by value instead
 of by reference. It is not a bug but bad code. In the example code the
 call is actually made as a call-by-reference, it just happens that I am
 using "d" twice in the same statement, and the compiler can evaluate the
 pieces of the statement any way it seems fit. In this case "d" gets
 evaluated before the side-effect of the call to "f" occurs. This is
 similar to the old textbook example of not using j=++i+i, where the
 results are system dependent.
 
 #include <iostream>
 using namespace std;
 
 double f(double& x)
 { x=1; return 2; }
 
 int main() {
    double d=5;
    // bad code: the line below produces false output of "2    5"
    cout << f(d) << "\t" << d << endl;
    // the line below produces correct output of "2    1"
    cout << f(d); cout << "\t" << d << endl;
 }
Comment 4 Andrew Pinski 2005-12-12 20:23:37 UTC
Reopening to ...
Comment 5 Andrew Pinski 2005-12-12 20:23:55 UTC
Mark as a dup of bug 11751.

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