Path: utzoo!attcan!uunet!ns-mx!iowasp!deimos!ux1.cso.uiuc.edu!brutus.cs.uiuc.edu!uakari.primate.wisc.edu!ames!sgi!shinobu!odin!warp.esd.sgi.com!kipp From: kipp@warp.esd.sgi.com (Kipp Hickman) Newsgroups: comp.lang.c++ Subject: Hairy C++ Question Keywords: constructors, operators Message-ID: <3734@odin.SGI.COM> Date: 7 Feb 90 21:34:01 GMT Sender: news@odin.SGI.COM Reply-To: kipp@warp.esd.sgi.com (Kipp Hickman) Organization: Silicon Graphics, Entry Systems Division Lines: 47 This source code caused C++ to generate a warning: Str s4 = s1 | s2 | s3; "timeStr.c++", line 57: warning: temporary used for non-const TempStr & argument ("|" means concatenation). What this turns into is (approx): Str s4( or( TempStr(s1,s2), s3 ) ) Now, there is only one temporary here because the "or" function is defined like thus: friend TempStr& operator|(TempStr&, const Str&); The expansion that uses that operator is what generates the warning. So here come the questions: 1. Why is this a problem? Is there a way that the temporary can be reused in a single expression that would cause a problem? If not, then the warning is bogus. If so, could I please see an example? 2. Why is this is a problem? If the compiler can notice that the temporary is being modified, it seems it can notice that another temporary could be generated? On a related subject, I had written my TempStr class without a TempStr(const TempStr&) constructor. What the compiler did in terms of code generation was entirely different, with and without this constructor. For example: class TempStr { public: friend TempStr operator|(const Str&, const Str&); }; The above example generated horrible structure copy code for the implementation of the operator| code. When I added the TempStr(const TempStr&) constructor, it stopped doing the copies, and instead wrote the function with an extra argument (which is what I wanted). It turns out that I don't even have to implement the TempStr(const TempStr&) constructor, because its *never* used (am I wrong). What gives? kipp