There is two different scenarios I’m after:
- You have a
shared_ptr - You have a
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&>(whereUis 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.