|
Boost : |
Subject: Re: [boost] rvalue ref best practices?
From: Dave Abrahams (dave_at_[hidden])
Date: 2012-06-12 12:05:17
on Mon Jun 11 2012, Mathias Gaunard <mathias.gaunard-AT-ens-lyon.org> wrote:
> On 11/06/2012 13:14, Giovanni Piero Deretta wrote:
>
>> The
>> only thing I can think of is assignment operators of derived classes
>> that need to forward to base classes. In that case I agree that pass
>> by value does not work very well (I usually still use pass by value,
>> but implement operator= in term of swap, which forwards to the base
>> class swap).
>
> According to this rule, I should pass by value whenever I want to copy
> the variable anyway.
>
> template<class T>
> struct foo
> {
> T t;
> foo(T t_) : t(t_) {}
> };
>
> Clearly we can see this code causes one copy with rvalues, two with
> lvalues, instead of always one if we used const-reference.
That's because you wrote it wrong. It should be
foo(T t_) : t(std::move(t_)) {}
> The only instance where this might be useful is operator=, and then
> again, it's probably still a bad idea.
>
> I had already pointed out to Dave that the way the rule as worded was
> incorrect.
? you did?
-- 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