Suppose I have an object which is managed by an std::unique_ptr
. Other parts of my code need to access this object. What is the right solution to pass the pointer?
Don't focus so much on the caller's requirements. What does the function need to be passed in? If it's only going to use the object for the duration of the call, then use a reference.
void func(const Foo& foo);
If it needs to retain ownership of the object past its duration, then pass in a shared_ptr
void func(std::shared_ptr foo);