Comparisons, Pros, Cons, and When to Use?
This is a spin-off from a garbage collection thread where what I thought was a simple answer generated a lot of comments about some specific smart pointer implementations so it seemed worth starting a new post.
Ultimately the question is what are the various implementations of smart pointers in C++ out there and how do they compare? Just simple pros and cons or exceptions and gotchas to something you might otherwise think should work.
I've posted some implementations that I've used or at least glossed over and considered using as an answer below and my understanding of their differences and similarities which may not be 100% accurate so feel free to fact check or correct me as needed.
The goal is to learn about some new objects and libraries or correct my usage and understanding of existing implementations already widely in use and end up with a decent reference for others.
C++03
std::auto_ptr
- Perhaps one of the originals it suffered from first draft syndrome only providing limited garbage collection facilities. The first downside being that it callsdelete
upon destruction making them unacceptable for holding array allocated objects (new[]
). It takes ownership of the pointer so two auto pointers shouldn't contain the same object. Assignment will transfer ownership and reset the rvalue auto pointer to a null pointer. Which leads to perhaps the worst drawback; they can't be used within STL containers due to the aforementioned inability to be copied. The final blow to any use case is they are slated to be deprecated in the next standard of C++.std::auto_ptr_ref
- This is not a smart pointer it's actually a design detail used in conjunction withstd::auto_ptr
to allow copying and assignment in certain situations. Specifically it can be used to convert a non-conststd::auto_ptr
to an lvalue using the Colvin-Gibbons trick also known as a move constructor to transfer ownership.On the contrary perhaps
std::auto_ptr
wasn't really intended to be used as a general purpose smart pointer for automatic garbage collection. Most of my limited understanding and assumptions are based on Herb Sutter's Effective Use of auto_ptr and I do use it regularly although not always in the most optimized way.C++11
std::unique_ptr
- This is our friend who will be replacingstd::auto_ptr
it will be quite similar except with the key improvements to correct the weaknesses ofstd::auto_ptr
like working with arrays, lvalue protection via private copy constructor, being usable with STL containers and algorithms, etc. Since it's performance overhead and memory footprint are limited this is an ideal candidate for replacing, or perhaps more aptly described as owning, raw pointers. As the "unique" implies there is only one owner of the pointer just like the previousstd::auto_ptr
.std::shared_ptr
- I believe this is based off TR1 andboost::shared_ptr
but improved to include aliasing and pointer arithmetic as well. In short it wraps a reference counted smart pointer around a dynamically allocated object. As the "shared" implies the pointer can be owned by more than one shared pointer when the last reference of the last shared pointer goes out of scope then the object will be deleted appropriately. These are also thread safe and can handle incomplete types in most cases.std::make_shared
can be used to efficiently construct astd::shared_ptr
with one heap allocation using the default allocator.std::weak_ptr
- Likewise based off TR1 andboost::weak_ptr
. This is a reference to an object owned by astd::shared_ptr
and will therefore not prevent the deletion of the object if thestd::shared_ptr
reference count drops to zero. In order to get access to the raw pointer you'll first need to access thestd::shared_ptr
by callinglock
which will return an emptystd::shared_ptr
if the owned pointer has expired and been destroyed already. This is primarily useful to avoid indefinite hanging reference counts when using multiple smart pointers.Boost
boost::shared_ptr
- Probably the easiest to use in the most varying scenarios (STL, PIMPL, RAII, etc) this is a shared referenced counted smart pointer. I've heard a few complaints about performance and overhead in some situations but I must have ignored them because I can't remember what the argument was. Apparently it was popular enough to become a pending standard C++ object and no drawbacks over the norm regarding smart pointers come to mind.boost::weak_ptr
- Much like previous description ofstd::weak_ptr
, based on this implementation, this allows a non-owning reference to aboost::shared_ptr
. You not surprisingly calllock()
to access the "strong" shared pointer and must check to make sure it's valid as it could have already been destroyed. Just make sure not to store the shared pointer returned and let it go out of scope as soon as you're done with it otherwise you're right back to the cyclic reference problem where your reference counts will hang and objects will not be destroyed.boost::scoped_ptr
- This is a simple smart pointer class with little overhead probably designed for a better performing alternative toboost::shared_ptr
when usable. It's comparable tostd::auto_ptr
especially in the fact that it can't be safely used as an element of a STL container or with multiple pointers to the same object.boost::intrusive_ptr
- I've never used this but from my understanding it's designed to be used when creating your own smart pointer compatible classes. You need to implement the reference counting yourself, you'll also need to implement a few methods if you want your class to be generic, furthermore you'd have to implement your own thread safety. On the plus side this probably gives you the most custom way of picking and choosing exactly how much or how little "smartness" you want.intrusive_ptr
is typically more efficient thanshared_ptr
since it allows you to have a single heap allocation per object. (thanks Arvid)boost::shared_array
- This is aboost::shared_ptr
for arrays. Basicallynew []
,operator[]
, and of coursedelete []
are baked in. This can be used in STL containers and as far as I know does everythingboost:shared_ptr
does although you can't useboost::weak_ptr
with these. You could however alternatively use aboost::shared_ptr<std::vector<>>
for similar functionality and to regain the ability to useboost::weak_ptr
for references.boost::scoped_array
- This is aboost::scoped_ptr
for arrays. As withboost::shared_array
all the necessary array goodness is baked in. This one is non-copyable and so can't be used in STL containers. I've found almost anywhere you find yourself wanting to use this you probably could just usestd::vector
. I've never determined which is actually faster or has less overhead but this scoped array seems far less involved than a STL vector. When you want to keep allocation on the stack considerboost::array
instead.Qt
QPointer
- Introduced in Qt 4.0 this is a "weak" smart pointer which only works withQObject
and derived classes, which in the Qt framework is almost everything so that's not really a limitation. However there are limitations namely that it doesn't supply a "strong" pointer and although you can check if the underlying object is valid withisNull()
you could find your object being destroyed right after you pass that check especially in multi-threaded environments. Qt people consider this deprecated I believe.QSharedDataPointer
- This is a "strong" smart pointer potentially comparable toboost::intrusive_ptr
although it has some built in thread safety but it does require you to include reference counting methods (ref
andderef
) which you can do by subclassingQSharedData
. As with much of Qt the objects are best used through ample inheritance and subclassing everything seems to be the intended design.QExplicitlySharedDataPointer
- Very similar toQSharedDataPointer
except it doesn't implicitly calldetach()
. I'd call this version 2.0 ofQSharedDataPointer
as that slight increase in control as to exactly when to detach after the reference count drops to zero isn't particularly worth a whole new object.QSharedPointer
- Atomic reference counting, thread safe, sharable pointer, custom deletes (array support), sounds like everything a smart pointer should be. This is what I primarily use as a smart pointer in Qt and I find it comparable withboost:shared_ptr
although probably significantly more overhead like many objects in Qt.QWeakPointer
- Do you sense a reoccurring pattern? Just asstd::weak_ptr
andboost::weak_ptr
this is used in conjunction withQSharedPointer
when you need references between two smart pointers that would otherwise cause your objects to never be deleted.QScopedPointer
- This name should also look familiar and actually was in fact based onboost::scoped_ptr
unlike the Qt versions of shared and weak pointers. It functions to provide a single owner smart pointer without the overhead ofQSharedPointer
which makes it more suitable for compatibility, exception safe code, and all the things you might usestd::auto_ptr
orboost::scoped_ptr
for.