Boost logo

Boost :

Subject: Re: [boost] C++03 unique_ptr emulation
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-01-09 10:43:15


David Abrahams wrote:
> on Thu Jan 08 2009, Ion Gaztañaga <igaztanaga-AT-gmail.com> wrote:
>
>> The same happens when implementing forwarding functions, so I'm starting to think that
>> putting T() in functions taking movable-only types by value is not a bad idea ;-)
>
> Sorry, but what do you mean by "putting T() in functions?"
>
> putting-milk-in-his-T'ly y'rs,

void function (movable m);

int main()
{
    movable m;
    function(movable(boost::move(m)));
    function(movable());
    return 0;
}

I mean that to pass movable-only objects per-value you need to put
movable() in the argument. This is needed if boost::move returns
boost::detail::rv<T> instead of T. Whether we return T or rv<T>,
returning a movable type must types must construct a movable object in
the return statement (see the attached test case and change the #define
BOOST_MOVE_RETURN_OPTION define to play with both approaches):

movable move_return_function ()
{
    if(cond){
       return movable();
    }
    else(cond){
       movable m;
       return movable(boost::move(m));
    }
}

This won't work in both cases:

movable move_return_function ()
{
     movable m;
     return movable;
}

Returning T from move() avoids the need of explicitly specify movable()
when passing arguments by value but makes forwarding really hard. On the
other hand returning rv<T> makes forwarding easy but passing by value
ugly. Of course, the question is if we can return something from move()
that can be different from T so that forwarding is easy but has no need
to specify movable() when passing by value.

Regards,

Ion




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