16

So, I'd like to use smart pointers instead of raw and almost every topic on SO says about Boost library. But std has such things as std::auto_ptr and std::shared_ptr. Why Boost? What is the difference?

It was a question not about difference of the implementation, but about reasons to use Boost pointers. I suppose, given answer, including date of answering and context, is reasonably useful. It helps to understand how Boost pointers were added to std.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • 1
    std::auto_ptr is deprecated btw – Denis Ermolin Nov 07 '12 at 10:37
  • The new smart pointers, like `std::shared_ptr` etc. (with the exception of `std::auto_ptr`) in C++11 were modeled after the structures with the same name in Boost. – Some programmer dude Nov 07 '12 at 10:39
  • 1
    Check the dates of those SO items you mention. Several `boost` smart pointers, such as `boost::shared_ptr`, were introduced in the standard only last year (and thus became `std::shared_ptr`). – Gorpik Nov 07 '12 at 10:40
  • Possible duplicate of [Differences between different flavours of shared\_ptr](https://stackoverflow.com/questions/1086798/differences-between-different-flavours-of-shared-ptr) – Trevor Boyd Smith Jun 12 '18 at 13:29

3 Answers3

27

Basically Boost did shared_ptr first. You may note that many of the new container classes in C++11 were in Boost long ago. I would expect this pattern to continue with the next revisions of the C++ standard, too. Boost supports older C++ compilers that don't talk C++11, which is a big benefit.

Incidentally, std::auto_ptr is deprecated in C++11, which brings in std::shared_ptr and std::unique_ptr instead, which are both significantly more useful.

Rook
  • 5,734
  • 3
  • 34
  • 43
  • 4
    More specifically: Boost is a library playground for authors to explore the design space and validate their libraries with real users. The C++ committee (which features many of such authors) then comes in and standardize what worked. – Matthieu M. Nov 07 '12 at 14:02
5

Checkout the following stackoverflow questions:

  1. Difference between boost::shared_ptr and std::shared_ptr from the standard <memory> file
  2. Differences between different flavours of shared_ptr
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
reece
  • 7,945
  • 1
  • 26
  • 28
4

Well, std::shared_ptr and boost:shared_ptr are both reference counting pointers. Instead std::auto_ptr works very differently. The difference between std::shared_ptr and boost:shared_ptr is very small and mostly historically. Before C++11 there was no std::shared_ptr and only boost:shared_ptr. When C++11 was designed, they took boost:shared_ptr as a model.

All your mentioned smart pointers have in common that they have their own mechanism to make sure that the lifetime management for points is done correctly. auto_ptr works so that if you have multiple instances of an auto_ptr then only one of them contains a pointer to the real object. Whenever you create an auto_ptr from another auto_ptr, then the new one will point to the object and the old one to NULL. On the other hand with shared_ptr there can be multiple shared_ptr instances that share the same object, only when the last one goes out of scope, only then the object is deleted..

In C++11 there is a similar pointer type to std::auto_ptr, namely std::unique_ptr, but there are some important differences, see also std::auto_ptr to std::unique_ptr.

References:

Community
  • 1
  • 1
H. Brandsmeier
  • 957
  • 5
  • 19