Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 909
moshbear Avatar asked Nov 20 '11 06:11

moshbear


2 Answers

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.

like image 133
Nicol Bolas Avatar answered Sep 22 '22 15:09

Nicol Bolas


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.

like image 45
Alok Save Avatar answered Sep 22 '22 15:09

Alok Save