64

Is the sole difference between boost::scoped_ptr<T> and std::unique_ptr<T> the fact that std::unique_ptr<T> has move semantics whereas boost::scoped_ptr<T> is just a get/reset smart pointer?

moshbear
  • 3,282
  • 1
  • 19
  • 33

2 Answers2

51

No, but that is the most important difference.

The other major difference is that unique_ptr can have a destructor object with it, similarly to how shared_ptr can. Unlike shared_ptr, the destructor type is part of the unique_ptr's type (the way allocators are part of STL container types).

A const unique_ptr can effectively do most of what a scoped_ptr can do; indeed, unlike scoped_ptr, a const unique_ptr cannot be rebound with a reset call.

Also, unique_ptr<T> can work on a T which is an incomplete type. The default deleter type requires that T be complete when you do anything to the unique_ptr that potentially invokes the deleter. You therefore have some freedom to play games about where that happens, depending on the situation.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Does `boost::scoped_ptr` require a fully defined type like `unique_ptr`? – Michael Price Nov 20 '11 at 06:17
  • So, the functionality chain would be scoped_ptr ->[custom del/alloc + move semantics]-> unique_ptr ->[copy semantics]-> shared_ptr? – moshbear Nov 20 '11 at 06:19
  • 7
    Michael, `unique_ptr` does not require a fully defined type—only the constructor/destructor do. This makes it usable for pimpl. – Cory Nelson Nov 20 '11 at 06:44
  • 9
    @MichaelPrice: `unique_ptr` doesn't require a fully defined type. `unique_ptr`'s destructor does. So does the destructor of a `scoped_ptr`. If you make one a member of a class, you can just give that class a destructor which is implemented in the .cpp file rather than the header. – Nicol Bolas Nov 20 '11 at 06:44
  • 11
    @moshbear: I'd say the functionality chain is "don't use `scoped_ptr` anymore." `unique_ptr` can do everything a `scoped_ptr` can do and more. A `const unique_ptr` is a `scoped_ptr`, but better. So really, there's no point in using `scoped_ptr` unless your compiler doesn't support `unique_ptr`. The fewer pointer templates you have, the better. – Nicol Bolas Nov 20 '11 at 06:47
  • 1
    There's one case where `scoped_ptr` is more useful than `const unique_ptr`: you can't reset pointers in `const unique_ptr`, but if the pointer is already at class scope, I might as well use regular `unique_ptr`. – moshbear Nov 20 '11 at 07:31
  • 2
    @NicolBolas maybe you could shift your comments on `unique_ptr` accepting incomplete types and `const unique_ptr` being a better `scoped_ptr` to your answer. I was about to add it as an amendment to your answer until I saw the comments. I think these are quite important facts. Nice answer regardless. – andreee May 16 '19 at 14:34
36

unique_ptr owns an object exclusively.It is non-copyable but supports transfer-of-ownership. It was introduced as replacement for the now deprecated auto_ptr.

scoped_ptr is neither copyable nor movable. It is the preferred choice when you want to make sure pointers are deleted when going out of scope.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 34
    I thought the preferred choice when you want to make sure that pointers are deleted at the end of a scope was `unique_ptr const`. – Mankarse Nov 20 '11 at 06:39
  • 9
    A bit late, but I think scoped_ptr does communicate the intention that a ptr must be deleted on scope exit more than unique_ptr. A unique_ptr can be moved out of a scope. – Brandon Kohn Sep 10 '14 at 13:17
  • 2
    @BrandonKohn: scoped_ptr supports `swap`, so it can also escape the scope. (Actually I have no idea why scoped_ptr forbids move but allows swap; this seems an inconsistency in the design.) – Nemo May 06 '21 at 14:21