Difference between boost::scoped_ptr and std::unique_ptr

前端 未结 2 1775
北恋
北恋 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: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 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.

提交回复
热议问题