Step 5 of 7
Moved-from objects
After b = std::move(a);, what is a? For standard library types the rule is valid but unspecified:
- You may destroy it, assign it a new value, or call functions with no preconditions (
clear(),empty(),size()). - You must not rely on its value. A moved-from
std::stringis usually empty, but nothing promises that.
So the professional habit is: after moving from something, either stop using it or reset it explicitly.
std::vector<std::string> batch = std::move(pending);
pending.clear(); // now it's definitely empty and ready to reuse
Your turn: write std::vector<std::string> take_all(std::vector<std::string>& src) that moves the whole contents out of src (no element copies) and leaves src definitely empty.
Previous: Return by value is free Next: Sink parameters and emplace_back