Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between boost::shared_ptr and std::shared_ptr from the standard <memory> file

Tags:

c++

c++11

boost

I was wondering if there are any differences between the boost::shared_ptr and the std::shared_ptr found in the standard <memory> file.

like image 262
Pepe Avatar asked Feb 04 '11 19:02

Pepe


People also ask

What is boost :: shared_ptr?

shared_ptr is now part of the C++11 Standard, as std::shared_ptr . Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array. This is accomplished by using an array type ( T[] or T[N] ) as the template parameter.

Why is shared_ptr unique deprecated?

this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.

Where is std :: shared_ptr defined?

If your C++ implementation supports C++11 (or at least the C++11 shared_ptr ), then std::shared_ptr will be defined in <memory> . If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++'s libstdc++).

What is shared_ptr?

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.


1 Answers

std::shared_ptr is the C++0x form of tr1::shared_ptr, and boost's boost::shared_ptr should behave the same.

However, std::shared_ptr, in an implementation that conforms to C++0x standard, should/might have more convenience behavior on the shared_ptr class, as described in the following links:

  • Differences between different flavours of shared_ptr

  • http://en.wikipedia.org/wiki/C%2B%2B0x#General-purpose_smart_pointers

The shared_ptr is a reference-counted pointer that acts as much as possible like a regular C++ data pointer. The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these.

  • November 2010 Working Draft of C++0x

Though from a quick cursory glance, I do not see operator+ and similar arithmetic operations on the shared_ptr type.

like image 153
逆さま Avatar answered Sep 24 '22 14:09

逆さま