move constructor and std::move confusion

前端 未结 2 1071
孤街浪徒
孤街浪徒 2021-02-08 02:21

I am reading about the std::move, move constructor and move assignment operator. To be honest, all I got now is confusion. Now I have a class:

class A{
  public:         


        
2条回答
  •  粉色の甜心
    2021-02-08 02:54

    1. B is the name of an object. Once a reference has been bound, it names an object. The distinction "rvalue reference", "lvalue reference" and "named object" only applies to how the name can be bound before you got this far.

    B.key is the name of a variable in the object which was supplied as argument to this function call.

    1. "invalidated" is not part of the standard terminology for moving. Standard library objects are left in an unspecified state after being moved out of; but that's not what's going on here.

    The line A.key = std::move(B.key) invokes the built-in definition of assignment for an int (this is a simple assignment, not a function call), which is just a copy. So B.key retains its value.

    1. For A(B()) to compile, B must be a typename which you haven't defined yet. (Did you mean A(A()) ? If so, then the answer is "Yes").

    2. See 2

    3. Use std::move(Z.foo) whenever you want to move out of Z.foo instead of copying from Z.foo.

提交回复
热议问题