[Solved] What is the advantage of passing by reference to shared_ptr over passing by reference directly


The only reason a function should accept a std::shared_ptr as a argument is if it may need to share or modify the ownership of the resource. If not, don’t pass a std::shared_ptr.

If the function will definitely need to take shared ownership then it should accept a std::shared_ptr by value. Only accept a std::shared_ptr& if the function may or may not take shared ownership.

If the function does not modify ownership then pass a reference to the resource, not a std::shared_ptr.

See: CppCoreGuidelines: F.7, R.30, R.34, R.35

solved What is the advantage of passing by reference to shared_ptr over passing by reference directly