73

I would like to enable support for C++0x in GCC with -std=c++0x. I don't absolutely necessarily 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 a few places where I use iterators, an auto type would be useful.

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

From what you know of the C++11 support, is it a good idea to enable it in GCC, and then embrace it by, for example, switching from using boost::shared_ptr to std::shared_ptr exclusively as the two don't mix?

PS: I'm aware of this good question which compares the different flavors of shared_ptr but I'm asking a higher level advice on which to use before the standard is finalized. Another way of putting that is, when a compiler like GCC says it supports an "experimental feature", does that mean I am likely to encounter weird errors during compilation that will be major time sinks and a source of cryptic questions on StackOverflow?

Edit: I decided to switch back from std::shared_ptr because I just don't trust its support in GCC 4.5 as shown by example in this question.

Community
  • 1
  • 1
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
  • 7
    I wonder if boost could just use a typedef to std::shared_ptr when using a compiler for which it's supported? – Mark Ransom Jun 12 '11 at 16:57
  • Having trouble finding the concrete question here? Voted to close. – user7116 Jun 13 '11 at 15:03
  • 4
    The question is clear in my opinion, it's just that the answer is not a simple yes or no. The answer requires a pro/con list, much like the one I marked as the answer. The question is essentially: Do you recommend std::shared_ptr be used before the standard is finalized? Why or why not? – Alan Turing Jun 13 '11 at 18:45

8 Answers8

62

There are a couple of reasons to switch over to std::shared_ptr:

  • You remove a dependency on Boost.
  • Debuggers. Depending on your compiler and debugger, the debugger may be "smart" about std::shared_ptr and show the pointed to object directly, where it wouldn't for say, boost's implementation. At least in Visual Studio, std::shared_ptr looks like a plain pointer in the debugger, while boost::shared_ptr exposes a bunch of boost's innards.
  • Other new features defined in your linked question.
  • You get an implementation which is almost guaranteed to be move-semantics enabled, which might save a few refcount modifications. (Theoretically -- I've not tested this myself) As of 2014-07-22 at least, boost::shared_ptr understands move semantics.
  • std::shared_ptr correctly uses delete [] on array types, while boost::shared_ptr causes undefined behavior in such cases (you must use shared_array or a custom deleter) (Actually I stand corrected. See this -- the specialization for this is for unique_ptr only, not shared_ptr.)

And one major glaring reason not to:

  • You'd be limiting yourself to C++11 compiler and standard library implementations.

Finally, you don't really have to choose. (And if you're targeting a specific compiler series (e.g. MSVC and GCC), you could easily extend this to use std::tr1::shared_ptr when available. Unfortunately there doesn't seem to be a 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
Community
  • 1
  • 1
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 5
    `typedef shared_ptr std::shared_ptr`? – GManNickG Jun 18 '13 at 20:03
  • 1
    @GManNickG: lol. Good catch. Damn typedef working backwards! (It feels like an assignment to me) – Billy ONeal Jun 18 '13 at 20:38
  • @GManNickG: Hmm... looking back at this maybe a using statement would be better. What do you think? – Billy ONeal Jun 18 '13 at 20:41
  • Yeah, a template `using` is correct: `template using shared_ptr = std::shared_ptr;`. The problem is that this is a C++11 feature, so you can't use it in the `boost::shared_ptr` case. :) The C++03 way is [another type with an internal typedef](http://stackoverflow.com/a/2795024/87234). – GManNickG Jun 18 '13 at 21:16
  • @GMan: you can't just do "using std::shared_ptr;", and bring that name into that namespace? – Billy ONeal Jun 18 '13 at 21:52
  • If you want to keep things simple, sure. :) – GManNickG Jun 19 '13 at 00:02
  • @GManNickG: Better? (Sorry, I don't have a boost installation to make sure this works with) – Billy ONeal Jun 19 '13 at 17:05
  • It may also be useful to add the same kind of support for `std::make_shared` / `boost::make_shared`. – BenC Jun 24 '13 at 02:47
14

I suppose it depends how much you use boost. I personally only use it very sparingly (in fact the random number library, in a single project). I've recently started using -std=c++0x for my other projects, and I use the new std:: library features like shared_ptr in them. I like my projects to have the minimum of dependencies, so I'd rather be dependent on the compiler's standard library implementation than on boost.

But I don't think there is a one-size-fits-all answer to this question.

13

You should always use std::shared_ptr wherever possible, if it's available, instead of boost. This is basically because all new interfaces which use shared_ptr will use the Standard shared ptr.

Puppy
  • 144,682
  • 38
  • 256
  • 465
7

It's probably not a bad idea to start getting into the habit of using std::shared_ptr when allowed by the compiler. Since the interface is the same as boost's shared_ptr you can always switch back if you needed to.

Chris Mennie
  • 564
  • 3
  • 7
4

Aside from implementation consistency, boost::shared_ptr currently retains at least two niche advantages over std::shared_ptr:

dhaffey
  • 1,354
  • 9
  • 12
4

I found std::shared_ptr to be faster than boost::shared_ptr. I did a test, you can review the code and see the pie chart results comparing boost, Qt, and std shared pointers.

enter image description here

https://www.osletek.com...

rosewater
  • 604
  • 2
  • 8
  • 22
  • 1
    I was surprised to see that the cases with malloc and new were so much faster. I don't think anyone would want to use the smart pointers if the performance penalty was a 10x slower call. Your test cases are not comparable: 1. malloc and new are only called once, allocating an array of size LENGTH, whereas in all other cases allocations are done LENGTH times, 2. for malloc and new you assign the result of rand to the allocated array, whereas in all other cases the result of the allocation being pushed back into a vector, leading to further memory allocations/reallocations. – andreas buykx Jun 25 '18 at 08:47
  • 3
    As a side note: pie charts are not a very useful way to show this kind of data. This article tells you why ... https://www.perceptualedge.com/articles/visual_business_intelligence/save_the_pies_for_dessert.pdf – andreas buykx Jun 25 '18 at 08:48
4

If you are just building on the one platform that is fine (make the switch)
(Note: You do have unit tests to validate backward compatibility don't you?)

If you compile on multiple platforms is where it becomes a little more awkward as you need to validate that the features on g++ 4.5 are available on all the platforms you use (ie building for MAC/Linux the default Mac g++ compiler is still a couple of version's behind the default compilers on Linux).

Martin York
  • 257,169
  • 86
  • 333
  • 562
4

Another reason to switch over to std::shared_ptr: it supports std::unique_ptr, i.e. has constructor.

boost::shared_ptr doesn't.

Bart
  • 2,062
  • 15
  • 19
papafi
  • 41
  • 1