C++ – passing references to std::shared_ptr or boost::shared_ptr

I found myself disagreeing with the highest-voted answer, so I went looking for expert opinons and here they are.
From http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything

Herb Sutter: “when you pass shared_ptrs, copies are expensive”

Scott Meyers: “There’s nothing special about shared_ptr when it comes to whether you pass it by value, or pass it by reference. Use exactly the same analysis you use for any other user defined type. People seem to have this perception that shared_ptr somehow solves all management problems, and that because it’s small, it’s necessarily inexpensive to pass by value. It has to be copied, and there is a cost associated with that… it’s expensive to pass it by value, so if I can get away with it with proper semantics in my program, I’m gonna pass it by reference to const or reference instead”

Herb Sutter: “always pass them by reference to const, and very occasionally maybe because you know what you called might modify the thing you got a reference from, maybe then you might pass by value… if you copy them as parameters, oh my goodness you almost never need to bump that reference count because it’s being held alive anyway, and you should be passing it by reference, so please do that”

Update: Herb has expanded on this here: http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/, although the moral of the story is that you shouldn’t be passing shared_ptrs at all “unless you want to use or manipulate the smart pointer itself, such as to share or transfer ownership.”

Leave a Comment