06
Value semantics and ownership
Four ownership shapes in one warehouse-dispatch graph, from Rule-of-Zero value types to unique, shared, and weak pointers.
A warehouse-dispatch object graph that makes the ownership choices concrete:
Parcelis a value type following the Rule of Zero (plain members, deep copies are independent).DockSlotholds sole ownership viaunique_ptr<Parcel>;transfer_parcel()moves a parcel between slots.CarrierContractfollows the Rule of Five (it owns a raw tier-rate array) and is shared acrossShippingRoutes viashared_ptr, so renegotiating through one route is visible to all routes sharing it.ManifestNodeowns its children viashared_ptrand observes its parent viaweak_ptr, breaking the cycle a bidirectionalshared_ptrwould create.
The C++23 codetabs path adopts a raw pointer from a C-style factory with
std::out_ptr; the C++20 path does it by hand. Both compile and test identically.
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 22/22 tests pass
./examples/06-value-semantics-ownership/demo.sh cpp23
The demo moves a parcel between dock slots, renegotiates a shared carrier
contract and shows every sharing route observe the change, and lets a manifest
parent expire while a child’s weak_ptr reports it gone. Chapter 6 walks
through when to reach for each ownership kind. Builds standalone and under the
cpp20 preset (22 tests).