Should I switch from using boost :: shared_ptr to std :: shared_ptr?

I would like to enable C ++ 0x support in GCC using -std=c++0x . I absolutely do not need any of the currently supported C ++ 11 features in GCC 4.5 (and soon 4.6), but I would like to start getting used to them. For example, in several places where I use iterators, the auto type would be useful.

But then again, I don't need any of the features currently supported. The goal here is to encourage me to include the features of the new standard in my programming vocabulary.

From what you know about C ++ 11 support, it’s useful to include it in GCC and then cover it with, for example, switching from boost::shared_ptr to std::shared_ptr only as two don’t mix?

PS: I know this good question , which compares the different tastes of shared_ptr , but I ask for a higher level of advice that I need to use before the standard ends. Another way to express this, when a compiler like GCC says it supports an “experimental function”, does this mean that I may encounter strange compilation errors, which will be the main time streams and the source of cryptic questions in StackOverflow?

Change I decided to switch from std::shared_ptr because I just do not trust its support in GCC 4.5 as shown in the example in this question .

+61
c ++ boost c ++ 11 stl shared-ptr
Jun 12 2018-11-12T00:
source share
7 answers

There are several reasons to switch to std::shared_ptr :

  • You remove the dependency on Boost.
  • debuggers. Depending on your compiler and debugger, the debugger may be “smart” near std::shared_ptr and show a directional object directly, where, for example, it did not speed up the implementation. At least in Visual Studio, std::shared_ptr looks like a simple pointer in the debugger, and boost::shared_ptr provides a bunch of inline strings.
  • Other new features defined in your related question.
  • You get an implementation that is almost guaranteed to include movement-semantics, which can save a few refcount modifications. (Theoretically - I did not test this myself) As of 2014-07-22, at least boost::shared_ptr understands the semantics of movement.
  • std::shared_ptr correctly uses delete [] for array types, whereas boost::shared_ptr causes undefined behavior in such cases (you should use shared_array or a custom debugger) (in fact, I am fixed. See this - specialization for this only for unique_ptr , not shared_ptr .)

And one important reason:

  • You limit yourself to the C ++ 11 compiler and standard library implementations.

Finally, you do not need to choose. (And if you are targeting a certain series of compilers (for example, MSVC and GCC), you can easily expand it to use std::tr1::shared_ptr when it is available. Unfortunately, there is no standard way to detect TR1 support)

 #if __cplusplus > 199711L #include <memory> namespace MyProject { using std::shared_ptr; } #else #include <boost/shared_ptr.hpp> namespace MyProject { using boost::shared_ptr; } #endif 
+55
Jun 12 2018-11-17T00:
source share

I suppose it depends on how much you use boost. I personally use it very sparingly (actually a library of random numbers in one project). I recently started using -std=c++0x for my other projects, and I am using new std :: library functions like shared_ptr. I like my projects with minimal dependencies, so I would rather depend on the implementation of the standard compiler library than on boost.

But I do not think that there is one answer to this question.

+13
Jun 12 2018-11-12T00:
source share

You should always use std::shared_ptr when possible, if available, instead of raising it. This is mainly because all new interfaces that use shared_ptr will use standard common ptr.

+13
Jun 12 '11 at 2:00
source share

It would probably be nice to get used to using std :: shared_ptr if allowed by the compiler. Since the interface is the same as boost shared_ptr, you can always switch back if you need to.

+7
Jun 12 2018-11-11T00:
source share

If you just build on the same platform, that's fine (make a switch)
(Note. You have unit tests to check for backward compatibility, right?)

If you compile on several platforms, this becomes a little more inconvenient, since you need to check that functions on g ++ 4.5 are available on all platforms that you use (for example, creating the Mac g ++ compiler for MAC / Linux by default is still somewhat default versions for default compilers on Linux).

+4
Jun 12 '11 at 16:50
source share

Another reason for switching to std::shared_ptr : it supports std::unique_ptr , i.e. has a constructor.

boost::shared_ptr does not work.

+4
Feb 25 '12 at 19:41
source share

In addition to the execution sequence, boost::shared_ptr currently retains at least two niche advantages over std::shared_ptr :

+4
Jun 18 '13 at 19:44
source share



All Articles