|
Boost : |
Subject: Re: [boost] [xint] Third release is ready, requesting preliminary review
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2010-05-04 06:25:59
On Mon, May 3, 2010 at 9:47 PM, Peter Dimov <pdimov_at_[hidden]> wrote:
> Chad Nelson:
>
>> If that's the case, and GCC is doing that as it should, why would adding
>> move semantics to the library provide any speed increase at all?
>
> Consider something like
>
> X f();
> g( X );
>
> int main()
> {
> f( g() ); // #1
> f( -g() ); // #2
> }
>
> In #1, the compiler can eliminate all copies, if f is written in a
> RVO-friendly way. (It won't be able to in general if there's more than one
> return statement, or the return value is a ternary ?: expression.)
>
> But in #2, there's going to be one allocation for the result of the unary
> op-. Even if operator- takes its parameter by value and directly flips its
> sign and returns it, I don't think that the compiler is allowed to allocate
> the return value and the parameter at the same address.
No, but swap is your friend:
X operator- (X x) {
modify-in-place x;
X ret; // this should be cheap
ret.swap(x);
return ret; // NRVO should kick in
}
HTH,
-- gpd
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk