Search code examples
c++move

Is moved variable valid to use after std::move?


I'm having hard time to understand if I std::move POD from one variable to another, is the source variable still valid to use or does it act something like dangling pointer? does it still point to stack memory?

for example:

int a = 5;
int b = std::move(a) // b owns a resources now

a = 10 // is this valid? does it have memory address?
std::cout << a; // prints 10 obviously valid?

Solution

  • std::move does nothing to a POD.

    int a = 5;
    int b = std::move(a);
    

    a is still good after that.

    For non-POD types, the moved object maybe valid for some operations and invalid for other operations -- it all depends on what the move constructor or move assignment operator does.