From: Smart Pointers to boost your code
unique_ptr
- unique ownership
- copying prevented
- use
std::moveto transfer ownership - const prevents transfer
- can’t be used as elements in containers
- use as replacement for
auto_ptr
shared_ptr
- reference counting
- referenced object is destroyed when (and only when) all copies of the
shared_ptrhave been destroyed - can be used as elements in containers
weak_ptr
- created as copy of
shared_ptr - copying/destroying have no effect on ref counting of
shared_ptr - after all copies of
shared_ptrhave been destroyed allweak_ptrcopies become empty
scoped_ptr (Boost)
- use
const unique_ptrinstead (orunique_ptrif you have to)
Other smart pointers (Boost)
intrusive_ptrshared_arrayscoped_array
Rules for smart pointers
- Assign and keep:
- Assign a newly constructed instance to a smart pointer immediately
- Keep the management of the referenced instance in the smart pointer(s).
- The smart pointer(s) own the object.
- Don’t delete the owned instance manually.
- You can’t (shouldn’t) take the instance away from the smart pointer.
..._ptr<T>is not aT*- There are no implicit conversions
- You can not assign a
T*to a..._ptr<T> - You can not write
ptr = NULLuseptr.reset() - To retrieve the raw pointer use
ptr.get() - You must not delete it!
- You must not use this pointer after the smart pointer(s) it comes from is/are destroyed, resetted or reassigned
- Avoid circular references
- Use
weak_ptrto break such cycles
- Use
- No temporary
shared_ptr- Always construct named smart pointer variables.
- No anonymous (i.e. as param in function call) smart pointer constuction
- Smart pointers are thread safe. But not necessary the pointee objects