Why does std::move take a forward reference?

后端 未结 2 538
鱼传尺愫
鱼传尺愫 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: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?

提交回复
热议问题