If I have a function that needs to work with a shared_ptr
, wouldn\'t it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr
It is sensible to pass shared_ptr
s by const&
. It will not likely cause trouble (except in the unlikely case that the referenced shared_ptr
is deleted during the function call, as detailed by Earwicker) and it will likely be faster if you pass a lot of these around. Remember; the default boost::shared_ptr
is thread safe, so copying it includes a thread safe increment.
Try to use const&
rather than just &
, because temporary objects may not be passed by non-const reference. (Even though a language extension in MSVC allows you to do it anyway)