Boost logo

Boost :

Subject: Re: [boost] rvalue ref best practices?
From: Dave Abrahams (dave_at_[hidden])
Date: 2012-06-12 12:18:45


on Sun Jun 10 2012, Ion Gaztañaga <igaztanaga-AT-gmail.com> wrote:

> El 09/06/2012 22:49, Giovanni Piero Deretta escribió:
>> On Sat, Jun 9, 2012 at 9:21 PM, Daniel Larimer<dlarimer_at_[hidden]>
>> wrote:
>>> I am trying to define my library API and have been using rvalue
>>> references and have observed some patterns that I would like to run
>>> by the community.
>>>
>>> In c++03 I would pass almost everything by const& unless it was to
>>> be modified. This results in extra copies any time a temporary is
>>> passed.
>
>> As per http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
>> , if the function is likely to consume or copy the parameter, the
>> best solution is to pass by value. This means you'll only need to
>> provide a single overload.
>
> What do you mean by "consume"? Passing by value is good if you are
> going to construct a new object in your implementation.

Or if you need to write on a copy of the contents of the actual
argument. That's what "consume" means.

> If you want to reuse resources of the passed object (memory from
> std::string, already constructed objects from a container...) passing
> by reference is the best choice.

Passing by reference always defeats copy elision. Why would you want to
do that?

> If you want to reuse resources from "*this", passing a const reference
> is the best way: implementing vector::operator=() with pass by value
> would be a terrible idea IMHO.

Right, because there's a much more efficient way to do it.

> You can catch temporaries by rvalue references to reuse external
> resources. For generic code, catching by value could do some premature
> pessimization.

Which pessimization is that?

> Passing by value has also another downside: it makes your binary
> interface dependent on the function implementation (do I copy the
> parameter?). If I change the implementation I need to break ABI.

I don't see how that follows. Leave the ABI alone. Pass-by-value
completely insulates the caller from the callee.

> I'm really worried about "pass by value is free" and "return by value
> is free" statements I'm hearing in some C++11 panels.

Where are you hearing that? I've heard Scott Meyers complain about that
too, but as far as I can tell, it's a misunderstanding of what's being
said.

[By the way "Want speed? Pass by value" is intended to be an
attention-grabbing title that stands conventional wisdom on its head,
not some sort of declaration that pass-by-value is always efficient.
The point is that for years people have compulsively avoided passing and
returning things by value for efficiency reasons, and it pays to
understand where copy elision kicks in (which it does on all major
compilers).]

> **************************************************************
> A bit off-topic: language extension to achieve maximum efficiency
> **************************************************************
>
> When thinking in solutions to this pass by value/reference issues, I
> feel C++ is missing some language feature so that a function can
> detect (at runtime, avoiding instantiations) if the output parameter
> is already constructed. Since function signature must unique, the
> caller should pass that information to the function in runtime.

An example of why, how, and where you want this would be helpful,
because I fail to grasp the motivation for something like this. Also, I
think you should take it to std-proposals_at_[hidden] :-)

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk