Difference between boost::scoped_ptr and std::unique_ptr

前端 未结 2 1776
北恋
北恋 2020-12-24 04:55

Is the sole difference between boost::scoped_ptr and std::unique_ptr the fact that std::unique_ptr has move

相关标签:
2条回答
  • 2020-12-24 05:16

    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.

    0 讨论(0)
  • 2020-12-24 05:25

    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.

    0 讨论(0)
提交回复
热议问题