Boost.Pointer容器在C ++ 11/14中被std :: unique_ptr淘汰了吗? [英] Boost.Pointer Container made obsolete by std::unique_ptr in C++11/14?
问题描述
std::unique_ptr
使 Boost.Pointer Container 库在C ++ 11/14中过时吗?
Does std::unique_ptr
make Boost.Pointer Container library obsolete in C++11/14?
在C ++ 98/03中没有移动语义,并且像shared_ptr
这样的智能指针具有与引用计数相关的 overhead (两者均用于ref计数块,而>互锁增量/减量).因此,与std::vector<T*>
相比,类似std::vector<shared_ptr<T>>
的开销很大.
In C++98/03 there isn't move semantics, and a smart pointer like shared_ptr
has reference-counting related overhead (both for the ref counting block, and the interlocked increment/decrements) if compared to raw pointers. So something like std::vector<shared_ptr<T>>
has overhead if compared to std::vector<T*>
.
但是std::vector<std::unqiue_ptr<T>>
与std::vector<T*>
(无引用计数开销)一样有效,另外和在安全方面异常和自动销毁(即vector<unique_ptr<T>>
析构函数将自动调用其指针存储在vector
中的T
项的析构函数)?
But is std::vector<std::unqiue_ptr<T>>
just as efficient as std::vector<T*>
(no reference counting overhead), and in addition safe in regard to exceptions and automatic destruction (i.e. vector<unique_ptr<T>>
destructor will automatically call the destructors for the T
items whose pointers are stored in the vector
)?
如果是这样,Boost.Pointer容器在C ++ 11/14代码中是否仍然有用,还是只是过时了?
If so, does Boost.Pointer Container still have a valid useful place in C++11/14 code, or is it just obsolete?
推荐答案
就像James在他的回答中提到的那样,与将unique_ptr
粘贴到标准库中所得到的容器相比,Boost.Pointer容器提供了更直观的界面.容器.
As James mentions in his answer, the Boost.Pointer containers offer a more intuitive interface as compared to what you get by sticking a unique_ptr
into a standard library container.
除此之外,boost::ptr_vector<T>
(和朋友)在下面将指向的类型存储为void *
,因此您不会获得每个T
的完整类模板实例化. vector<unique_ptr<T>>
并非如此.
Aside from that, boost::ptr_vector<T>
(and friends) store the pointed to type as a void *
underneath, so you don't get an entire class template instantiation for every T
. This is not the case with vector<unique_ptr<T>>
.
这篇关于Boost.Pointer容器在C ++ 11/14中被std :: unique_ptr淘汰了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!