boost: scoped_ptr和std::unique_ptr

时间:2021-07-25 18:55:30

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?

唯一的区别是boost::scoped_ptr 和std::unique_ptr , std::unique_ptr 具有移动语义,而boost::scoped_ptr 只是一个get/reset智能指针?

2 个解决方案

#1


38  

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).

另一个主要区别是unique_ptr可以有一个析构函数对象,就像shared_ptr可以那样。与shared_ptr不同,析构函数类型是unique_ptr类型的一部分(分配器是STL容器类型的一部分)。

#2


25  

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.

unique_ptr只拥有一个对象。它是不可复制的,但支持所有权的转移。它被引入作为已废弃的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.

scoped_ptr既不可复制也不可移动。当您想要确保指针在超出范围时被删除时,这是首选。

#1


38  

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).

另一个主要区别是unique_ptr可以有一个析构函数对象,就像shared_ptr可以那样。与shared_ptr不同,析构函数类型是unique_ptr类型的一部分(分配器是STL容器类型的一部分)。

#2


25  

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.

unique_ptr只拥有一个对象。它是不可复制的,但支持所有权的转移。它被引入作为已废弃的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.

scoped_ptr既不可复制也不可移动。当您想要确保指针在超出范围时被删除时,这是首选。