Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move semantics std::move

I don't understand very well the std::move function

template <class T>
typename remove_reference<T>::type&&
move(T&& a)
{
    return a;
}

why remove_reference ? could someone give me a simple explanation ?

like image 347
Guillaume Paris Avatar asked Apr 03 '11 11:04

Guillaume Paris


1 Answers

Think about what happens if T is an lvalue reference, for example MyClass &. In that case, T && would become MyClass & &&, and due to reference collapsing rules, this would be transformed into MyClass & again. To achieve the right result, typename remove_reference<MyClass&>::type&& first removes any reference decorations from the type, so MyClass & is mapped to MyClass, and then the rvalue reference is applied to it, yielding MyClass &&.

like image 188
fredoverflow Avatar answered Nov 01 '22 03:11

fredoverflow