Re:Mistakes to avoid with C++ 11 smart pointers
by hellofunk on Sunday May 15, 2016 @03:41AM
(#11699932)
I agree, and I frequently use unique_ptr only, and then pass naked pointers to non-owning objects who need a reference. In this way, the code is clear: if it is a regular pointer, it is non-owning always.
Some might argue that using shared pointers is better for this application, but then you must also deal with potential reference cycles and add weak pointers to the mix eventually, and in addition to these concerns, handle any runtime hits for the reference counting.
Re:Mistakes to avoid with C++ 11 smart pointers
by barrkel on Sunday May 15, 2016 @05:49AM
(#11700184)
Life is easy when there's an unambiguous single owner. Shared pointers are really for the harder situation where ownership is distributed. I prefer to use arenas if possible, when objects live in a complicated graph.
Re:Mistakes to avoid with C++ 11 smart pointers
by zanny on Sunday May 15, 2016 @02:11PM
(#11702000)
It is definitely better to use T& instead of shared_ptr<T>&. Passing references to either unique or shared pointers is just adding a layer of indirection you are never going to use, unless you intend to copy-construct the pointer object later, which sounds more like you need to rethink your object design if you want to do something that messy.
Re:Mistakes to avoid with C++ 11 smart pointers
by hellofunk on Sunday May 15, 2016 @03:23PM
(#11702318)
To be clear, I was not referring to references at all but rather to naked pointers you get by calling get() on a unique_ptr, and passing those to non-owning classes/functions that need them (including third-party APIs).
Your point is still valid, though -- a shared_ptr should be copied only when you actually want to increase the reference count.
In general, I try to avoid shared_ptr unless I really need reference counting. unique_ptr is much easier to reason about and is much more efficient.