From -4305765414448154793
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,f5d433881cc29cff
X-Google-Attributes: gidf78e5,public
From: David Vandevoorde <daveed@vandevoorde.com>
Subject: Re: Named return value optimisation- reprise.
Date: 1997/06/15
Message-ID: <33A43847.DFAD6F89@vandevoorde.com>#1/1
X-Deja-AN: 248724510
References: <33a345bf.7425526@news.ipswich.gil.com.au>
X-Original-Date: Sun, 15 Jun 1997 14:45:27 -0400
X-Priority: 3 (Normal)
Organization: -
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBM6SpKEy4NqrwXLNJAQHLHAH9EXkMUnMZ+LiI/kEZ+j89apaF7d/7EzR/ bhR6Cyz9dbvHNW3KYBWSv9TfhvoY/hjAcm+H5cKLPcebhuDqv5c4eg== =yaDS
Reply-To: daveed@vandevoorde.com
Newsgroups: comp.std.c++
Originator: austern@isolde.mti.sgi.com


Brian Parker wrote:
[...]
> MV_Vector_double Copy_C_array()
> {
>         MV_Vector_double A(a, 3, MV_Vector::ref);
>         return A;
> }

Writing a copy-function this way is asking for trouble:
there is no copy-function visible in this function (and
the standard does not mandate an invisible one).

[...]
> Note that the same problem occurs with the return value optimisation
> i.e. if Copy_C_array() was written as-
> 
> MV_Vector_double Copy_C_array()
> {
>         return MV_Vector_double(a, 3, MV_Vector::ref);
> }

Same thing: you never requested a copy of your temporary
object, so you cannot reasonably expect one on every
implementation. If you want a copy, ask for one:

	return MV_Vector_double(
	          MV_Vector_double(a, 3, MV_Vector::ref));

> This example is of the same form as my previously posted example, but
> it is interesting as it occurs using a well-known matrix package.

Well yes, but I'm sure you won't argue that the example
function you posted is not reasonable? The problem is
not with the MV++ package per se.

> Although the draft otherwise only guarantees that values are copied
> one or more times on pass-by-value or return-by-value, that is a
  ^^^
Where does it guarantee this? [stmt.return] certainly doesn't,
and [expr.call] makes an explicit caveat that the copy may be
folded into a conversion operation.

> benign form of non-determinism compared with the semantics-altering
> behaviour of zero or more copies that [class.copy] paragraph 15
> introduces.

``By value'' means that a value as opposed to a reference is
used to cross a call-boundary. It does not mean that a copy 
must be made using a copy-ctor for that  purpose (otherwise,
it would probably have been called ``by copy'').

C++ offers the less deterministic mechanism to those situations
that can deal with it (and reap performance benefits as a 
consequence), and a simple syntax to enforce a ``by copy''
approach when necessary (or convenient).

	Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



