29

As I understand it, in the current specification of C++11, one should use:

  • std::unique_ptr<> for one owner (most of the time)
  • std::shared_ptr<> only when there are multiple owners in acyclic structure
  • std::weak_ptr<> sparingly only when there are cycles that need to be broken
  • A raw pointer as a handle to memory (no ownership) when a reference would not suffice

So my questions are:

  1. Are these policies sufficient or are there additional policies that I should be aware of?
  2. Are scoped_ptr<> and auto_ptr<> effectively obsolete?
ildjarn
  • 62,044
  • 9
  • 127
  • 211
kfmfe04
  • 14,936
  • 14
  • 74
  • 140

1 Answers1

32

Are scoped_ptr<> and auto_ptr<> effectively obsolete?

auto_ptr is deprecated in C++11, so there's your answer. scoped_ptr doesn't exist in C++11 and never did. The main reason to use boost::scoped_ptr is to ensure that ownership is never transferred (unless you cheat, of course). Then again, if you use unique_ptr, ownership can only be transferred if you use std::move or similar constructs. Or, as Howard points out, just make it a const std::unique_ptr.

So it's really up to you whether you want that extra bit of insurance. Also boost::scoped_ptr doesn't have deleter support. So unique_ptr can play tricks that boost::scoped_ptr cannot.

std::weak_ptr<> sparingly only when there are cycles that need to be broken

I can't say I agree with this policy necessarily. A weak_ptr should be used when an object may want to talk to something else, but it doesn't own that something else. Which means that it can be deleted at any time, and the holder of the weak_ptr needs to be able to handle that deletion anytime it tries to talk to it.

Breaking cycles is one of the uses of weak_ptr; it should not be the only time it is used.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 8
    +1 With additional information: A `const unique_ptr` makes a stronger statement that ownership won't transfer out of scope than does a `boost::scoped_ptr`. The latter can swap out of scope whereas the former can not. – Howard Hinnant Dec 01 '11 at 01:32
  • 1
    @HowardHinnant: just out of curiosity, what are cases where you would want to allocate on the heap for something that's so short-lived? If the object is somehow too big for the stack? – kfmfe04 Dec 01 '11 at 09:17
  • 3
    @kfmfe04: I don't find myself using `const unique_ptr` (or `boost::scoped_ptr`). I do use a local `unique_ptr` a fair amount, but that is usually for exception safety reasons: to safely hold a resource while I obtain other resources, and then transfer the resource from the local `unique_ptr` to another more permanent object. That being said, I know `boost::scoped_ptr` has many fans and I'm sure their use cases are valid. – Howard Hinnant Dec 01 '11 at 14:12
  • also, you could fill a container with `unique_ptr`s, allowing them to be shuffled or reorganized more quickly than if they were holding objects, but not need to worry about deleting them before removing them from the containers as you would if they were holding raw pointers. – NHDaly Jun 11 '13 at 20:11