DEV Community

Discussion on: Memory management using Smart Pointers in C++ - Part 2

Collapse
pratikparvati profile image
Pratik Parvati Author

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.

Collapse
sandordargo profile image
Sandor Dargo

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...