-6

Someone made question "should I pass shared_ptr by reference" and he got this reply which has plenty upvotes. https://stackoverflow.com/a/8385731/5543597

It makes me wonder why he has so many upvotes, and if it's true what he is saying:

That depends on what you want. Should the callee share ownership of the object? Then it needs its own copy of the shared_ptr. So pass it by value.

If a function simply needs to access an object owned by the caller, go ahead and pass by (const) reference, to avoid the overhead of copying the shared_ptr.

The best practice in C++ is always to have clearly defined ownership semantics for your objects. There is no universal "always do this" to replace actual thought.

If you always pass shared pointers by value, it gets costly (because they're a lot more expensive to copy than a raw pointer). If you never do it, then there's no point in using a shared pointer in the first place.

Copy the shared pointer when a new function or object needs to share ownership of the pointee.

Especially here:

Should the callee share ownership of the object? Then it needs its own copy of the shared_ptr. So pass it by value.

Why creating copy of shared_ptr by passing by value, when it could be reference and callee could just make copy of shared_ptr using reference he received, once he decide to store it in his data?

Or this:

If you never do it, then there's no point in using a shared pointer in the first place.

When passing it by refence to functions, it still exists in parent function. And once decided to store it, it can be stored without problems.

Are these statements correct?

Community
  • 1
  • 1
John Lock
  • 623
  • 1
  • 7
  • 18
  • 1
    This seems a bit meta. If your question is, "What do you think," then we need to close this as primarily opinion based. Do you have a specific technical question? If so, please put it prominently in your post. – John Zwinck May 22 '16 at 02:57
  • my question is if these technical statements are correct – John Lock May 22 '16 at 03:44
  • "make copy of shared_ptr using reference he received" this is somewhat more expensive than just passing by copy, but you certainly can do this if the callee only needs to make a copy sometimes. – n. m. could be an AI May 22 '16 at 03:52
  • @n.m. why more expensive? You say that passing shared_ptr by reference to functions/methods, and making copy of shared_ptr to store it as data memeber in some occassions, is more expensive than passing it always by value? – John Lock May 24 '16 at 09:08

1 Answers1

2

Yes that answer is correct.

Why creating copy of shared_ptr by passing by value, when it could be reference and callee could just make copy of shared_ptr using reference he received, once he decide to store it in his data?

You could do that. But passing by value serves as self-documentation that the callee might will make a copy. Also, passing by value has the advantage that the function can be called with an rvalue and moved out of, e.g. func( make_shared<T>() );

M.M
  • 138,810
  • 21
  • 208
  • 365
  • `But passing by value serves as self-documentation that the callee might will make a copy` Well, shared_ptr isn't magic, and its not different from other objects. So you say that, passing any object(even large objects) serves as self-documentation that the callee might will make a copy? – John Lock May 24 '16 at 09:15
  • @JohnLock Typically, yes – M.M May 24 '16 at 09:39