move semantics std::move

后端 未结 2 1181
北海茫月
北海茫月 2021-02-19 23:32

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

template 
typename remove_reference::type&&
move(T&& a)         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 00:12

    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::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 &&.

提交回复
热议问题