🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3001

Re: Q: returning object from function

#3001
From: Steve Clamage
Date: Wed, 10 Jan 1996 23:08
42 lines
2209 bytes
In article 5ig@dub-news-svc-4.compuserve.com,
100754.2730@compuserve.com (Martin Aupperle) writes:
>I have 

>  X  x = f();

>with some non-trivial class X. If I understand the DWP (12.2:
>temporary objects) right, it is implementation dependent whether 
>- a temporary is constructed (on the stack) that is used to initialize
>x
>- the result is constructed in x's memory directly. 

>The difference is the construction of the temporary. If my observation
>is true, there a strong requirement concerning the behaviour of the
>copy constructor, if one wants to write portable software.  

>Is that true? 

In many kinds of expressions, the compiler is allowed to introduce or
to optimize away the creation and deletion of a temporary variable.
Your example is one of those cases. Another is
	X x = y;
which literally means
	X x(X(y));
but which may be interpreted as
	X x(y);

In such cases, the rules of C++ say in effect that the semantics of the
program are the same whether the temporary is created or not. Thus, you
have to design your classes so that program correctness does not depend
on whether extra temporaries are created. 

Usually that means that constructors and destructors should have
complementary side effects, and you have to be careful about acquiring
locks or other scarce resources in a copy constructor.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <4d0obc$il8@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4cvsq0$5ig@dub-news-svc-4.compuserve.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 10 Jan 96 23:08:30 GMT
Approved: fjh@cs.mu.oz.au
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Q: returning object from function
X-Original-Date: 10 Jan 1996 16:09:16 GMT
Organization: Sun Microsystems Inc.
Message-ID: <4d0obc$il8@engnews1.Eng.Sun.COM>
References: <4cvsq0$5ig@dub-news-svc-4.compuserve.com>
Reply-To: clamage@Eng.Sun.COM
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMPRG/+EDnX0m9pzZAQELXwF/Qn9/sF4tOWwhq/069q4AFYnkrVYyUOcH
 cGIDHoAK1gHPgbG/ctkVtlD77HQ9990c
 =WQuk