Date: Sat, 28 Apr 2001 16:15:09 +0200 From: Raymond Wiker <Raymond.Wiker@fast.no> To: freebsd-stable@FreeBSD.ORG Subject: Re: g++ bug in FreeBSD-4.3 Message-ID: <15082.53357.634513.762317@raw.grenland.fast.no> In-Reply-To: <200104281611170258.00DFFF78@tensor.xs4all.nl> References: <15082.46019.913297.264758@raw.grenland.fast.no> <200104281611170258.00DFFF78@tensor.xs4all.nl>
next in thread | previous in thread | raw e-mail | index | archive | help
Dimitry Andric writes: > > Anyway, I came across the following bug report: > ><URL:http://gcc.gnu.org/ml/gcc-bugs/2001-04/msg00707.html>. This > >looks pretty serious. > > Sorry, but this is not a bug. The expression: > > cout << f(d) << "\t" << d << endl; > > is equivalent to: > > (((cout << f(d)) << "\t") << d) << endl; > > which is equivalent to: > > > (((cout.operator<<(f(d))).operator<<("\t")).operator<<(d)).operator<<( > endl); > > And then you come to the nice part of the story: operator '.' is NOT > required to evaluate its operands in any particular order! What most > compilers do with this type of code is produce something like the > following (in pseudo-assembly): > > // push arguments back-to-front: > push endl; > push d; // (*) here the value is still 5 > push "\t"; > push d; // still 5 > call f; // here d's value is modified to 1 > // call operator<<'s, popping stack as you go > call operator<<(double); // for f(d) > call operator<<(const char*); // for "\t" > call operator<<(double); // for d, which is 5, see (*) > call operator<<(endl); // for endl > > And this will lead to the results from the bug report. It not only > happens with g++, but also with VC++ under Win32, for example. > > The problem is that you should not put code with side effects (such > as calling f() in this case) in cout << ... expressions. This will > lead only to unexpected results. :) Damn :-) I was hoping I had found the reason that my code had stopped working, but that does not appear to be the case. Oh well, I guess I'll have to get back to debugging again, then. Thanks to you, and to Erik Trulsson, for putting me straight on this. //Raymond. -- Raymond Wiker Raymond.Wiker@fast.no To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15082.53357.634513.762317>