Should/Can smart pointers be passed by reference in functions?
Ie:
void foo(const std::weak_ptr<bar>& x)
Should/Can smart pointers be passed by reference in functions?
Ie:
void foo(const std::weak_ptr<bar>& x)
Of course you can pass a smart-pointer by const&
.
There is also a reason to do so:
If the function accepting said smart-pointer is just a front for one accepting a raw (observing) pointer for manipulating the (potential) pointee.
Never force anyone to use a smart-pointer for anything but transfering / sharing ownership, they might not be managing it with the one you insist on.
In all other cases, using a smart-pointer implies transfer / sharing of ownership, and avoiding a cheap copy (just a test whether it owns anything and reference-counting, as well as two pointer copies) doesn't actually buy you anything.
No, if we are talking about std
smart pointers. const T &
can't transfer/share ownership, so raw non-owning T *
is always better and more efficient.