Checkpoint: Ownership Transfer
intermediate35 min readLesson 111 of 204
An Inventory whose take() moves a unique_ptr out of a vector — the exact mechanics of ownership leaving a container.
take() is the exam because it is the precise dance ownership teaches:
- find the slot by name
auto out = std::move(*it);— ownership leaves the vector; the slot now holds an emptyunique_ptritems_.erase(it);— remove the empty husk- return
out
Skipping step 2 destroys the item; skipping step 3 leaves a nullptr hole in the container. Both mistakes are silent at compile time — which is why the tests check size, identity, and containment after the call.
This is the same dance std::vector::erase-with-move, node removal in
linked structures, and job queues all perform. Learn it once here and you
will recognize it everywhere.