Boost logo

Boost Users :

Subject: Re: [Boost-users] [Review] Formal Review: Boost.Move
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2010-05-24 02:58:31


On 23/05/2010 21:09, Steven Watanabe wrote:
> I'm wondering whether it would be better to
> use something like
>
> template<class T>
> struct rv {
> T* impl;
> operator const T&() const;
> };
>
> template<class T>
> T& unwrap_rv(rv<T>&);

This does not seem to work:

template<class T>
struct rv_ref
{
    rv_ref(T &t){ ptr = &t; }
    rv_ref(const T &t){ ptr = &t; }

    operator const T& () const { return this->get(); }
    T& get() { return *const_cast<T*>(ptr); }
    const T& get() const { return *ptr; }

    private:
    const T *ptr;
};

template<class T>
inline rv_ref<T> move(T &t)
{ return static_cast< rv_ref<T> >(t); }

class movable
{
    movable(movable&);
    movable & operator=(movable&);
    public:
    operator rv_ref<movable>()
       { return rv_ref<movable>(*this); }
    operator const rv_ref<movable>() const
       { return rv_ref<movable>(*this); }

    movable() : i(0) {}

    movable(rv_ref<movable> rv)
    {
       movable &t = rv.get();
       i = t.i;
       t.i = 0;
    }

    movable& operator=(rv_ref<movable> rv)
    {
       movable &t = rv.get();
       i = t.i;
       t.i = 0;
       return *this;
    }
    int i;
};

movable sm_rvalue()
{
    movable m;
    return move(m);
}

int main()
{
    movable m, m2(move(m));
    m = move(m2);
    //MSVC 7.1
    //error C2679: binary '=' : no operator found which
    //takes a right-hand operand of type 'movable' (or
    //there is no acceptable conversion)
    //GCC
    //error: no matching function for call to 'movable::movable(movable)'
    //note: candidates are: movable::movable(rv_ref<movable>)
    //note: movable::movable(movable&)
    m = sm_rvalue();
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net