From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-04-24 06:59:13
>From: "Noel Yap" <Noel.Yap_at_[hidden]>
> Terje Slettebø wrote:
> > The compiler may elide such copy, even if the temporary being returned
> > named variable, and several compilers (such as g++ and the EDG based
> > does this optimisation (RVO). For example:
> Playing devil's advocate, I think RVO can easily be fooled with
> not-so-tricky code. If you're really concerned about this situation,
> use some sort of smart pointer. For example:
> boost:shared_ptr< T > f( boost::shared_ptr< T > const in_ );
The part about RVO was really concerned with the out() scenario, not
in_out(). I'm not sure if passing a smart pointer buys you very much. In
this case, the smart pointer is const but the pointee is not, so the "const"
in the signature is really just masking what is going on.
Furthermore, this means dynamical allocation (either by caller or callee),
which may be more expensive than making a copy on the stack (unless it's
elided), and passing that.
Yes, there are ways to write it so that RVO is disabled, but with careful
coding for this, such as using single exit, you can enable it, and it can be
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk