Taking a shared_ptr
by r-value reference is unnecessary. If done on purpose, it might imply that unique_ptr
is a better
choice since it transfers unique ownership.
In general, a function should take:
- A copy of a
shared_ptr
, if the function takes part in the ownership of the managed object
- A reference to a
shared_ptr
if the function plans to modify the shared_ptr itself by calling reset
or
swap
- A reference to a
const
shared_ptr
, if the function might take part in the ownership by copying the reference to
another shared_ptr on at least one of its paths
- A raw pointer/reference to the object, if the function is only interested in the current value of the managed object
Noncompliant code example
Class Circle{};
void fn(shared_ptr<Circle>&& circle); // Noncompliant
Compliant solution
Class Circle{};
void fn(shared_ptr<Circle> circle);
Exceptions
The move constructor of the shared_ptr
itself.