1) When you delete a derived class through a base class pointer where the base class has no virtual destructor, that is not a partial deletion. It's undefined behaviour which is much worse. More info here.
2) Passing around a smart pointer by reference to gain on the performance is not a good idea. We've seen this with a colleague of mine and we found that the reason behind is a lack of understanding.
Why?
What's the purpose of using a smart pointer?
It's to handle ownership in a safe way. That's why you use smart pointers. If you pass around a reference, you don't pass/share the ownership.
If ownership is not involved, there is no need for smart pointers. Instead, you should just pass around a pointer.
It will be even faster because there is less indirection. Plus you don't have to make the mental exercise of understanding what a reference of a pointer mean.
I understand your point on ownership semantics of the objects; It completely makes sense to pass smart pointers by value. My intension was to point out the overhead added by the atomic operations (increment and decrement reference count) in shared_ptr; perhaps, I should edit and make this point clear.
What should be made clear IMO is that there is no good reason to pass a smart pointer by reference.
If you refer to Herb Sutter, he recommends that you "don’t pass a smart pointer as a function parameter unless you want to use or manipulate the smart pointer itself, such as to share or transfer ownership".
Consider that and knowing that sharing a smart pointer by reference doesn't share or transfer the ownership, we have no reasons to pass a SP by reference.
Thanks for the article.
I have two remarks.
1) When you delete a derived class through a base class pointer where the base class has no virtual destructor, that is not a partial deletion. It's undefined behaviour which is much worse. More info here.
2) Passing around a smart pointer by reference to gain on the performance is not a good idea. We've seen this with a colleague of mine and we found that the reason behind is a lack of understanding.
Why?
What's the purpose of using a smart pointer?
It's to handle ownership in a safe way. That's why you use smart pointers. If you pass around a reference, you don't pass/share the ownership.
If ownership is not involved, there is no need for smart pointers. Instead, you should just pass around a pointer.
It will be even faster because there is less indirection. Plus you don't have to make the mental exercise of understanding what a reference of a pointer mean.
QuickBench here.
(You don't need an inner loop in QuickBench. With
for (auto _ : state)
it takes care of executing things enough)Thanks for your remarks Sandor.
I understand your point on ownership semantics of the objects; It completely makes sense to pass smart pointers by value. My intension was to point out the overhead added by the atomic operations (increment and decrement reference count) in shared_ptr; perhaps, I should edit and make this point clear.
What should be made clear IMO is that there is no good reason to pass a smart pointer by reference.
If you refer to Herb Sutter, he recommends that you "don’t pass a smart pointer as a function parameter unless you want to use or manipulate the smart pointer itself, such as to share or transfer ownership".
Consider that and knowing that sharing a smart pointer by reference doesn't share or transfer the ownership, we have no reasons to pass a SP by reference.
Some corresponding rules:
isocpp.github.io/CppCoreGuidelines...
isocpp.github.io/CppCoreGuidelines...