What is std::move(), and when should it be used?

后端 未结 8 2087
粉色の甜心
粉色の甜心 2020-11-22 08:27
  1. What is it?
  2. What does it do?
  3. When should it be used?

Good links are appreciated.

8条回答
  •  滥情空心
    2020-11-22 09:00

    std::move itself does nothing rather than a static_cast. According to cppreference.com

    It is exactly equivalent to a static_cast to an rvalue reference type.

    Thus, it depends on the type of the variable you assign to after the move, if the type has constructors or assign operators that takes a rvalue parameter, it may or may not steal the content of the original variable, so, it may leave the original variable to be in an unspecified state:

    Unless otherwise specified, all standard library objects that have been moved from being placed in a valid but unspecified state.

    Because there is no special move constructor or move assign operator for built-in literal types such as integers and raw pointers, so, it will be just a simple copy for these types.

提交回复
热议问题