Boost logo

Boost :

Subject: Re: [boost] rvalue ref best practices?
From: Daniel Larimer (dlarimer_at_[hidden])
Date: 2012-06-09 18:17:06

On Jun 9, 2012, at 4:49 PM, Giovanni Piero Deretta wrote:

> 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++11 I would pass anything that I plan on 'storing' or 'consuming' by &&. This results in the most efficient code, but 'breaks' lvalue compatibility and forces me to consider having two (or more) versions of every method. Unfortunately, you get a explosion of permutations of rvalue and const& as the number of arguments increases and it makes getting function pointers much more 'ugly'.
>> Instead of providing two (or more) overloads, one for lvalues and one for rvalue, I have decided to only provide the rvalue overload and if the user wants to pass an lvalue they wrap with std::ref(), std::cref(), std::move() or my own helper copy() which makes the copy explicit and converts the lvalue to a rvalue. (std::ref() and std::cref() could only work if the arguments are template parameters... in which case the method would probably use std::forward<>() to handle the auto-detection of move vs copy semantics.
> As per ,
> 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.
> -
I only partially grasped the article the first time I read it (before posting my original message).... but in light your response I revisited it to see what I missed and you are right. Pass by value does almost exactly what I want *except* that copies are still implicit and I might accidentally 'copy' a large object when I didn't intend to. I would never know this without profiling.

So, how much value is there to 'explicit copy only' types?

Boost list run by bdawes at, gregod at, cpdaniel at, john at