Are there any differences between boost::shared_ptr
, std::tr1::shared_ptr
and the upcoming (in C++0x
) std::shared_ptr
?
Will porting from one to another have any overhead or are they basically the same?
In short: Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.
Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.
shared_ptr are noticeably slower than raw pointers. That's why they should only be used if you actually need shared ownership semantics. Otherwise, there are several other smart pointer types available. scoped_ptr and auto_ptr (C++03) or unique_ptr (C++0x) both have their uses.
The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.
According to the Boost website, the boost::shared_ptr
...
...conforms to the TR1 specification, with the only exception that it resides in namespace
boost
instead ofstd::tr1
.
According to the Wikipedia C++0x page
The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these.
If your code works with the TR1/Boost version, it should work with the C++0x version (but not necessarily vice versa).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With