Hacker Newsnew | past | comments | ask | show | jobs | submit | sb10128's commentslogin

Second agadmator, lots of personality and humor along with the detailed analysis.


Can you explain what you mean here? Isn't what the op is doing generally ok - to pass shared_ptr's by const reference to save an additional increment/decrement on function call?

https://stackoverflow.com/questions/3310737/shared-ptr-by-re...


It isn't idiomatic. If you've already locked the shared_ptr, you should extract it's boxed value and pass that by reference directly. Passing a shared_ptr by reference has fairly niche applications and allows the callee to do things like reset the shared_ptr, extract a weak_ptr from it, etc.

That said, if you are in need of the latter use case, it certainly should be passed by reference. It's not just a reference count! Shared pointers in C++ are threadsafe so there's a fair bit more going on under the hood that makes copying it (more) expensive.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: