Difference between boost::scoped_ptr<T> and std::unique_ptr<T>

moshbear picture moshbear · Nov 20, 2011 · Viewed 30.1k times · Source

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?

Answer

Nicol Bolas picture Nicol Bolas · Nov 20, 2011

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.