There is two different scenarios I'm after:
shared_ptr
unique_ptr
The answer might be the same though.
Consider a method, which uses a pointer but does not assume ownership:
void use_pointer(T ptr)
{
ptr->act();
}
Should T be
my_type *
(raw pointer)const shared_ptr<my_type> &
(sending const ref, if using shared_ptr)const unique_ptr<my_type> &
(sending const ref, if using unique_ptr)weak_ptr<my_type>
(constructing weak_ptr for method call)Something else? Thanks!
If you do not assume ownership, then preferably accept a reference to the pointee. If you want to express optionality, you can use a raw pointer, or you can use e.g. boost::optional<U&>
(where U
is the pointee type).
Functions that do no assume ownership should almost never accept a smart pointer. There is in general nothing useful for the callee in a smart pointer interface other than means of getting to the pointee. So pass that instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With