4 ms·
> It passes shared_ptr via reference Talk about not getting it.
by fnj 9y ago
> It passes shared_ptr via reference
Talk about not getting it.
- sb10128 9y agoCan 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-reference-or-by-value https://stackoverflow.com/questions/3310737/shared-ptr-by-re...
- banachtarski 9y agoIt 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.