Boost logo

Boost :

Subject: Re: [boost] rvalue ref best practices?
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2012-06-11 07:09:40


On Sat, Jun 9, 2012 at 11:03 PM, Howard Hinnant
<howard.hinnant_at_[hidden]> wrote:
>> 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.
>
> "The best solution" has an implicit "always" in it.  Telling programmers they don't have to think, design or measure is a disservice to our industry.  Pass-by-value is a good tool to have in the toolbox.  It is not always the best solution.  There is no solution that is always best.

Of course it isn't, as it wasn't the existing rule "pass objects of
class type by const reference", but is a safe default.

[snip test]
> For me this runs about 19% slower one way than the other.  And I have no idea how portable that result is.  Maybe for your application the 19% speed hit (more or less) is inconsequential.  Maybe for someone else's application a 19% speed hit (more or less) is critical.  <shrug>  We the advise givers have no idea.
>

I didn't actually believe this, so I tested it (gcc 4.6) and you are
right. I couldn't measure the actual overhead because the variance on
my tests was very high, but the overhead was definitely there. It is
surprising, because even with the rvalue copy there is an explicit
temporary there.

> Pass-by-value is a good tool in the toolbox.  It is not free.  There are other good tools in the toolbox too.  None of them are free either.

Agree.

-- 
gpd

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