Resource management
R.34
Take a `shared_ptr<widget>` parameter to express shared ownership
Reason
This makes the function's ownership sharing explicit.
Example, good
class WidgetUser
{
public:
// WidgetUser will share ownership of the widget
explicit WidgetUser(std::shared_ptr<widget> w) noexcept:
m_widget{std::move(w)} {}
// ...
private:
std::shared_ptr<widget> m_widget;
};
Enforcement
- (Simple) Warn if a function takes a
Shared_pointer<T>parameter by lvalue reference and does not either assign to it or callreset()on it on at least one code path. Suggest taking aT*orT&instead. - (Simple) ((Foundation)) Warn if a function takes a
Shared_pointer<T>by value or by reference toconstand does not copy or move it to anotherShared_pointeron at least one code path. Suggest taking aT*orT&instead. - (Simple) ((Foundation)) Warn if a function takes a
Shared_pointer<T>by rvalue reference. Suggesting taking it by value instead.