Question

i wrote a simple function that return object rvalue reference.But its not working right.But if i use std::move its working , i see only difference is remove_reference or what is? or my casting to rvalue is wrong ?

template<class T>
T&& mymove(T &&obj)
{
    return (T&&)obj;
}

template<class _Ty> inline
    typename remove_reference<_Ty>::type&&
        move(_Ty&& _Arg) _NOEXCEPT
    {   // forward _Arg as movable
    return ((typename remove_reference<_Ty>::type&&)_Arg);
    }
Was it helpful?

Solution

What you got in mymove is universal reference - not rvalue.

The rule of reference collapsing is this :

T&& -> T&&
T&& & -> T&
T& && -> T&
T&& && -> T&&

If you pass a lvalue to mymove, you are going to get lvalues both as argument and return type. That is why your mymove is not working in the same way as std::move works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top