Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2002-02-04 19:08:14


On Monday, February 4, 2002, at 06:25 PM, Rainer Deyke wrote:

> What about return-by-move? I see two possibilities:
>
> 1. Use the same syntax:
>
> T f()
> {
> T a;
> return std::move(a);
> }
>
> T b(f());
>
> This requires special compiler support to prevent 'a' from being
> destroyed before 'b' is constructed.
>
> 2. Let the compiler handle it automatically:
>
> T f()
> {
> T a;
> return a;
> }
>
> T b(f());
>
> Here the compiler realizes that 'a' will be destroyed immediately
> after 'b' is constructed, so it uses the movement constructor instead
> of the copy constructor if both are available.

Actually I believe the standard already allows return value optimization
to make:

T b(f());

work as desired. But here's another possibility in case your compiler
is weak in this area:

T b(std::move(f()));

(assuming we can get the move from temporary worked out)

-Howard


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