views:

155

answers:

4

When a function should take a boost::shared_ptr, are you passing it

by const reference void foo(const boost::shared_ptr<T>& p)

or by value void foo(boost::shared_ptr<T> p) ?

I would prefer the first method because I suspect it to be faster. But is this really worth a though or are there any additional issues?

Edit: Could you please give the reasons for your choice or if the case, why you think that it does not matter.

+7  A: 

Personally I would use a const reference. There is no need to increment the reference count just to decrement it again for the sake of a function call.

Evan Teran
Go with the `const` reference
Shakedown
Why the downvote? This question essentially boils down to a matter of preference. I gave a reasonable explanation for my preference...
Evan Teran
I did not down-vote your answer, but before this is a matter of preference, there are pros and cons to each of the two possibilities to consider. And it would be good to know and discuss theses pros and cons. Afterwards everyone can make a decision for himself.
Danvil
@Danvil: taking into consideration of how `shared_ptr` works, the only possible downside to not passing by reference is a slight loss in performance. There are two causes here. a) the pointer aliasing feature means to pointers worth of data plus a counter (maybe 2 for weak refs) is copied, so it is slightly more expensive to copy the data round. b) atomic reference counting is slightly slower than plain old increment/decrement code, but is needed in order to be thread safe. Beyond that, the two methods are the same for most intents and purposes.
Evan Teran
A: 

Pass by const reference, it's faster. If you need to store it, say in some container, the ref. count will be auto-magically incremented by the copy operation.

Nikolai N Fetissov
A: 

I'd pass it by reference, if only because of std::auto_ptr semantics:

When you do

void foo(std::auto_ptr<bar> p);

the behaviour is very different from

void foo(std::auto_ptr<char> const&p);

which is what you usually expect. Passing by reference is what the caller usually expects, passing by reference has a surprising behavior, and many people will fall in the trap.

As a rule, I always pass smart pointers by const reference.

Alexandre C.
No bad things don't happen. The auto_ptr does exactly what it is supposed to do and passes ownership to the function. When you call a function and pass an auto_ptr the function is telling you I will take ownership of the pointer so don;t expect it back. If the function decides to retain it then the function will do some work storing it internally otherwise the object will be destroyed. The point is that once passed the caller can not use the pointer as they no longer have it. This is what I always loved about auto_ptr the explicit nature of who owns the object and when it was being given up.
Martin York
Of course, but it is not always what you want. And it is often what you _don't_ want when you use auto_ptr for RAII as is often advised in C++ books (they don't mention ` boost::scoped_ptr` )
Alexandre C.
But `auto_ptr` is not `shared_ptr` - they *don't* share the same semantics.
Georg Fritzsche
A: 

shared_ptr isn't large enough, nor do its constructor\destructor do enough work for there to be enough overhead from the copy to care about pass by reference vs pass by copy performance.

stonemetal