Two Conceptual Questions: i) (Smart) Pointers vs. References ii) How to return objects from functions?

OPEN

Hi, I'm in the process of learning C++ and I'm a little confused about two things. Hoping the reddit community can help me out.

 

i. When to use smart pointers vs references: Is it the general "rule of thumb" to just use references until you can't anymore? Source. Or is it more preferred to pass objects by (smart) pointer?

 

ii. How to return objects from functions (i.e. created on the stack)? Is passing a return parameter frowned upon? Or, is it better to just return by copy and let the compiler optimize?

 

Thank you very much!

Posted Jun 22, 2019, 4:32 AM UTC · edited

89
abstracted8•

I'm not super experienced in c++ but these are the guidelines I try to follow.

  • only return a smart pointer it you are signaling that you are transferring ownership of the pointed to object to the caller.
  • if you still need to return a pointer that points to something that is owned by something else, use raw pointers.
  • if it returns an stl container, return by value since return value optimization should always kick in and nothing gets copied.
  • never return a reference to a variable created inside the function, that is undefined behaviour as the stack wipes all of the locals but the returned reference still exists.
  • remember that pointers can point to nothing but references can't, you can use this sometimes to express intent.
5

if you still need to return a pointer that points to something that is owned by something else, use raw pointers.

In this scenario, how does the user of the pointer know that the pointer is still valid (since the pointed to object is owned by something else)?

1
calomer•

Exactly. Return a pointer if pointed object will outlive the purpose. Otherwise, things get complicated. Depends on use case.. If you need access to the object after the current holder / owner is done with it, then either smart pointers or refactoring is required.

3
Norbivar•

You can't. Although this would only be a case in legacy systems, but that is a whole other story for "what to/not to do". The only effective solution would be to use sharedptr, if you must really follow such a scenario.

1
kisielk•

1 and 2 wouldn’t that be unique vs shared pointers? As opposed to smart vs raw.

0
calomer•

Raw pointer signals access intent, whereas shared ptr signals (shared) ownership. In general your interface that uses / modifies an object but doesn't buffer it should use raw pointers.

1
HappyFruitTree•

If the object is a member of the class or is passed in by reference to the function you might want to return a reference to avoid copying for objects that are costly to copy.

If the object is created as a local variable, or as a temporary object, inside the function I think the general recommendation is to return it by value. In most cases the compiler can elide the copy and if that fails it will try to implicitly move the object which is often relatively performant.

If the object is costly to move (e.g. a large std::array), or costly to construct each time, you might want to consider instead passing it as an argument, by reference.

Returning a smart pointer should not be your first choice...

If the function dynamically allocate the object for some reason then you probably want to return it as a std::unique_ptr.

If the object is stored inside the class (or elsewhere) as a std::shared_ptr and you want to offer shared ownership to the caller then you could return a std::shared_ptr (by reference, to avoid unnecessary reference counting overhead).

If you don't want to offer ownership of the object to the caller then you just return a reference (or a copy) no matter how it's stored.

5
alfps•

When there is no lifetime management issue or other consideration that forces smart pointers, introducing a smart pointer just adds indirection and possible overhead for no gain.

References versus (raw) pointers is a thornier issue.

For arguments references help to write expression oriented code. For example, foo( Something() ); instead of Something smth; foo( &smth );. Also any reference is known to not be null in a valid program, so one avoids some checking of whether a pointer is null.

Still, if a function stores a referring pointer to an argument, it can be more clear if that argument is passed as a pointer than as a reference. More clear, to me, because one then passes exactly what the function needs, a pointer, instead of something (a reference) that it can use to obtain what it needs. However, someone else might find it more unclear, because the pointer signals that it may be null.

In the end there is no ideal, no simple set of rules.

But don't introduce complexity and/or possible inefficiency/overhead and/or header dependencies for no good reason.

4
calomer•

https://youtu.be/ZbVCGCy3mGQ

I believe this talk goes over everything you ask for.

3