Boost logo

Boost :

Subject: Re: [boost] Boost.Move Where we are with move semantic emulation on C++03 compilers?
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-02-12 16:36:59


David Abrahams wrote:
> Oh, Eric posted a link to the fast one (that credits Paul's work).

Thanks Dave and Eric for the link. Until now in my libraries I was using
a forwarding like this (warning, fast witten, not compiled code)

//move
template<class Movable>
boost::rv<Movable> & move (Movable &m);

//forward
template<class RvalueRef>
RvalueRef & forward
   ( const RvalueRef &r
   , typename enable_if<is_boost_rv<RvalueRef> >::type = 0 )
{
    return const_cast<RvalueRef &>(r);
}

template<class T>
const T &forward
   ( const T &r
   , typename disable_if<is_boost_rv<T>>::type = 0 )
{
    return r;
}

//emplace function of a container
//Simple preprocessor expansion:

template<class T0, class T1, ...>
void emplace_back(const T0 &t0, const T1 &t1, ....)
{
    addr = allocate();
    new(addr)(boost::forward<T0>(t0), boost::forward<T1>(t1), ...);
}

so I could do something like:

handle get_handle();
movable_only get_movable_only()

{
    Container<movable_only> container;
    movable_only m(get_movable_only());
    container.emplace(get_handle(), move(m));
    //This does not compile
    container.emplace(get_movable_only(), move(m));
    //This fowards r as a const reference
    container.emplace(r);
}

Non-const references were not forwarded, but this usually was enough for
common container use cases (constructors taking non-const references are
not usual). I don't know if this would be acceptable for a move
emulation library or a complete overload set would be required.

Another possibility is to add a restriction and use boost::ref to pass
non-const references so forward() could detect them.

template<class T>
T &forward
   ( const boost::reference_wrapper<T> &r
   , typename disable_if<is_boost_rv<T>>::type = 0 )
{
    return r.get();
}

Thanks again for the link,

Ion


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