Move semantics (C++11) transfer resources from one object to another without deep copying—critical for std::vector, std::string, and custom RAII types.
std::move
std::vector<int> a = {1, 2, 3};
std::vector<int> b = std::move(a);
// a is valid but empty/unspecified
std::move casts to rvalue reference—it does not move by itself; move constructors/assignments perform the transfer.
Rule of five
If you manage resources manually, consider destructor, copy/move constructors, and copy/move assignment. With rule of zero, rely on RAII members.
Important interview questions and answers
- Q: lvalue vs rvalue?
A: lvalues have identity and persist; rvalues are temporaries or moved-from objects eligible for move. - Q: When is move automatic?
A: Returning locals, inserting temporaries—compiler elision and move often apply without explicitstd::move.
Self-check
- What does std::move actually do?
- Name the rule-of-five special members.
Tip: Do not std::move from an object you still need—moved-from state is valid but unspecified.
Interview prep
- What does std::move do?
Casts to rvalue reference to enable move construction or assignment—it does not move by itself.
- Rule of five?
Destructor, copy/move constructor, copy/move assignment when manually managing resources.