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
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)'. 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 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; } Reopening to ... |