move semantics std::move

后端 未结 2 1162
北海茫月
北海茫月 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:11

    Because rvalue reference to lvalue reference would decay to lvalue reference, and returing lvalue reference would have different semantics from those you would expect from move.

    Edit: Huh, why the downvote? Check out this code:

    template < typename T > T&& func(T&& x) { return x; }
    
    int main()
    {
            int x;
    
            int &y = func(x);
    }
    

    Further reading: http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html

提交回复
热议问题