1

I am still having a hard time understanding why

void work_with_foo(shared_ptr<foo> _foo);

is not recommended compared to

void work_with_foo(foo* _foo)

I understand that work_with_foo(foo& _foo) is better, but a pointer as a parameter! What if someone calls delete _foo?

Can someone explain me why? I always use shared_ptr, so for example, in my code...

shared_ptr<foo> ptr_foo(new foo);
//calling work_with_foo which takes foo pointer
work_with_foo(ptr_foo); //will  this even work?
rubenvb
  • 74,642
  • 33
  • 187
  • 332
fromCtoCpp
  • 11
  • 2
  • Swings and roundabouts - a function should be allowed to assume its arguments remain valid for the length of the call. – Alex Chamberlain Jun 13 '13 at 13:17
  • This will give an extensive answer to your question: [Herb Sutter: GotW #92, Smart Poitners as Parameters](http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/) – Arne Mertz Jun 13 '13 at 13:18
  • 2
    "What if someone calls `delete _foo`?" What if someone calls `delete _foo.get()` on your `shared_ptr`? In a codebase that consistently uses smart pointers, it should be understood that raw pointers never have ownership - they function as a reference that might be null, or as an iterator over a raw array. Nothing else. – Sebastian Redl Jun 13 '13 at 13:21

3 Answers3

4

If the function being called is only operating on an existing object and will not be taking (shared) ownership, passing by shared_ptr is a bit misleading. It would also have an additional cost of reference counting compared to passing an object referece.

Mark B
  • 95,107
  • 10
  • 109
  • 188
2

Actually, if the function you want to pass the object to doesn't require any ownership transfer, the best way is:

void work_with_foo(const foo& _foo);

or make that

void work(const foo& _foo);

And at the call site, dereference the shared_ptr.

If your function needs to modify the object pointed to, remove the const. Note this also works with base class pointers and inheritance etc.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
0

Why do you think passing a shared_ptr is "not recommended"? Who recommends against it?

The one thing that's important to note is that passing a shared_ptr by const-ref may be preferable, in that you will avoid incrementing the reference count before and decrementing it after calling the function. So perhaps passing a shared_ptr by value is not great in some cases, but that doesn't mean you need to fall back to raw pointers.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Passing a `shared_ptr` object around implies copying ownership, and incurs a penalty otherwise not necessary. – rubenvb Jun 13 '13 at 13:18
  • 1
    What penalty does it incur (if you pass a shared_ptr by const-ref)? – John Zwinck Jun 13 '13 at 13:22
  • I wasn't talking about passing a reference to it. You imply no one would recommend against passing the `shared_ptr` object around. I disagree. The penalty of passing a `shared_ptr` by value is the refcount update, which may or may not block threads if the object is being accessed concurrently. – rubenvb Jun 13 '13 at 17:29