Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass smart pointers into functions?

Tags:

When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory?

When I pass, for example, a std::vector<std::string> into a function I always consider the following options:

  1. I'm going to change the state of the vector object, but I do not want those changes reflected after the function has finished, AKA make a copy.

    void function(std::vector<std::string> vec); 
  2. I'm going to change the state of the vector object, and I do want those changes reflected after the function has finished, AKA make a reference.

    void function(std::vector<std::string> & vec); 
  3. This object is pretty big, so I'd better pass a reference, but tell the compiler not to let me change it.

    void function(std::vector<std::string> const& vec);   

Now is this the same logic with smart pointers? And when should I consider move semantics? Some guidelines on how I should pass smart pointers is what I desire most.

like image 630
Trevor Hickey Avatar asked Sep 20 '12 19:09

Trevor Hickey


People also ask

Can you pass a smart pointer to function?

Reason Passing a smart pointer transfers or shares ownership and should only be used when ownership semantics are intended (see R. 30). Passing by smart pointer restricts the use of a function to callers that use smart pointers. Passing a shared smart pointer (e.g., std::shared_ptr) implies a run-time cost.

Should smart pointers be passed by reference?

It's ok to pass a smart pointer by reference, except if it's to a constructor. In a constructor, it's possible to store a reference to the original object, which violates the contract of the smart pointers.

Can you pass a std :: unique_ptr as a parameter to a function?

You cannot do that because unique_ptr has a move constructor but not a copy constructor. According to the standard, when a move constructor is defined but a copy constructor is not defined, the copy constructor is deleted.

How does C++ implement smart pointers?

Features. In C++, a smart pointer is implemented as a template class that mimics, by means of operator overloading, the behaviors of a traditional (raw) pointer, (e.g. dereferencing, assignment) while providing additional memory management features.


1 Answers

Smart pointers have pointer semantics, not value semantics (well, not the way you mean it). Think of shared_ptr<T> as a T*; treat it as such (well, except for the reference counting and automatic deletion). Copying a smart pointer does not copy the object it points to, just like copying a T* does not copy the T it points to.

You can't copy a unique_ptr at all. The whole point of the class is that it cannot be copied; if it could, then it wouldn't be a unique (ie: singular) pointer to an object. You have to either pass it by some form of reference or by moving it.

Smart pointers are all about ownership of what they point to. Who owns this memory and who will be responsible for deleting it. unique_ptr represents unique ownership: exactly one piece of code owns this memory. You can transfer ownership (via move), but in so doing, you lose ownership of the memory. shared_ptr represents shared ownership.

In all cases, the use of a smart pointer in a parameter list represents transferring ownership. Therefore, if a function takes a smart pointer, then it is going to claim ownership of that object. If a function isn't supposed to take ownership, then it shouldn't be taking a smart pointer at all; use a reference (T&) or if you have need of nullability, a pointer but never store it.

If you are passing someone a unique_ptr, you are giving them ownership. Which means, by the nature of unique ownership, you are losing ownership of the memory. Thus, there's almost no reason to ever pass a unique_ptr by anything except by value.

Similarly, if you want to share ownership of some object, you pass in a shared_ptr. Whether you do it by reference or by value is up to you. Since you're sharing ownership, it's going to make a copy anyway (presumably), so you might as well take it by value. The function can use std::move to move it into class members or the like.

like image 164
Nicol Bolas Avatar answered Oct 07 '22 09:10

Nicol Bolas