std::move (C++)

When you call std::move() on an object, it casts that object to an rvalue reference.

Seen in CS247.

data{std::move(other.data)}
  • std::move is included in <utility>, forces an lvalue to be treated like an rvalue. Move Constructor runs.

Copy pasted from https://stackoverflow.com/questions/3413470/what-is-stdmove-and-when-should-it-be-used-and-does-it-actually-move-anythi

std::move is used to indicate that an object t may be “moved from”, i.e. allowing the efficient transfer of resources from t to another object.

std::move on primitive types? Not much efficiency gain

How does std::move actually work under the hood?

I thought that it basically ensures that the destructor doesn’t get called, but actually it does sometimes still! (source).

Move construction or move assignment can actually be copies! In fact, they will be copies if the type being moved happens to be a pre-C++11 type with a copy constructor or a copy assignment.

Even if a class has a move constructor or a move assignment it may choose that it can’t move its guts to the new object, e.g., because the allocators mismatch.