RAII
auto target = std::make_unique<Target>(Target{"local", 443});
check(target->port == 443, "unique_ptr");
No new
No delete
No leak when an exception leaves the function
The idea
Acquire a resource in a constructor Release it in a destructor Scope exit does the cleanup, including on error paths
unique_ptr
One owner Moveable, not copyable Use it by default for heap allocation
shared_ptr
Shared ownership with reference counting
Cycles leak
Use weak_ptr to break a cycle
If you cannot explain why ownership is shared, it probably should not be
make functions
std::make_unique and std::make_shared are the normal forms
They keep allocation and construction together and avoid a few exception safety traps
Next
Containers in 08 containers
Posted