Why does std::move take a forward reference?

后端 未结 2 522
鱼传尺愫
鱼传尺愫 2020-12-16 02:37

The implementation of std::move basically looks like this:

template
typename std::remove_reference::type&&
mo         


        
相关标签:
2条回答
  • 2020-12-16 02:47

    Here is some example simplified to the extreme:

    #include <iostream>
    #include <vector>
    
    template<typename T>
    T&& my_move(T& t)
    {
        return static_cast<T&&>(t);
    }
    
    int main() 
    {
        std::vector<bool> v{true};
    
        std::move(v[0]); // std::move on rvalue, OK
        my_move(v[0]);   // my_move on rvalue, OOPS
    }
    

    Cases like the one above may appear in generic code, for example when using containers which have specializations that return proxy objects (rvalues), and you may not know whether the client will be using the specialization or not, so you want unconditional support for move semantics.

    0 讨论(0)
  • 2020-12-16 02:53

    It doesn't hurt.

    You're simply establishing a guarantee that code will treat the result as an rvalue. You certainly could write std::move in such way that it errors out when dealing with something that's already an rvalue, but what is the benefit?

    In generic code, where you don't necessarily know what type(s) you're going to be working with, what gains in expressiveness would you extract out of a bunch of "if type is rvalue do nothing else std::move" plastered everywhere when you can simply say "I promise we can think of this as an rvalue".

    You said it yourself, it is nothing more than a cast. Should *_cast operations also fail if the argument already matches the intended type?

    0 讨论(0)
提交回复
热议问题