Resource management
R.21
Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership
Reason
A unique_ptr is conceptually simpler and more predictable (you know when destruction happens) and faster (you don't implicitly maintain a use count).
Example, bad
This needlessly adds and maintains a reference count.
void f()
{
shared_ptr<Base> base = make_shared<Derived>();
// use base locally, without copying it -- refcount never exceeds 1
} // destroy base
Example
This is more efficient:
void f()
{
unique_ptr<Base> base = make_unique<Derived>();
// use base locally
} // destroy base
Enforcement
(Simple) Warn if a function uses a Shared_pointer with an object allocated within the function, but never returns the Shared_pointer or passes it to a function requiring a Shared_pointer. Suggest using unique_ptr instead.