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中过时?
在C++98/03中,没有移动语义,并且与原始指针相比,像shared_ptr
这样的智能指针具有与引用计数相关的开销(对于引用计数块和互锁的增量/递减)。因此,与std::vector<T*>
相比,像std::vector<shared_ptr<T>>
这样的东西有开销。
但是,在异常和自动销毁方面,std::vector<std::unqiue_ptr<T>>
是否与std::vector<T*>
(无引用计数开销)、和一样高效,以及安全(即vector<unique_ptr<T>>
析构函数将自动调用指针存储在vector
中的T
项的析构函数)?
如果是这样的话,Boost.Pointer容器在C++11/14代码中是否仍然有一个有效的有用位置,或者它只是过时了?
它不是obdelete;它有一个完全不同的比CCD_ 10更直观的界面。
James在回答中提到,与将unique_ptr
插入标准库容器相比,Boost.Pointer容器提供了更直观的界面。
除此之外,boost::ptr_vector<T>
(和朋友)将指向的类型存储为下面的void *
,因此不会为每个T
获得整个类模板实例化。vector<unique_ptr<T>>
的情况并非如此。
尝试使用std::vector<std::unqiue_ptr<T>>
struct Foo {
int a;
};
vector<unique_ptr<Foo>> bar;
bar.push_back(make_unique<Foo>(1));
cout << bar[0]->a << endl; // rvalue, is ok
Foo *foo = bar[1].get(); // try to use a pointer, this interface "bar[1].get()" is awful
指针容器当然有更直观的界面。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 为什么 std::unique 不调用 std::sort?
- 我对 std::unique(算法)C++有问题
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++ std::unique并没有显示我对它的期望
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误
- 为什么 [std::unique] 不能应用于 [std::multiset]?
- 使用 std::unique 和 vector.erase 删除除最后一次出现的重复元素之外的所有元素
- 是否可以在 std::unique<T[ ]> 上应用 std::sort?
- std::unique 是否有大小限制
- 从 std::unique 返回的迭代器的用法
- 使 std::unique<T> 与 std::unique<const T, CustomDeleterType 兼容>
- Std::list.unique()应该使迭代器无效
- 无法使用初始化器列表构造 vector<std::unique<...>>
- 从自定义对象- std:unique的向量中消除重复项会导致崩溃
- 我能保证std::unique将保留第一个元素吗?
- 在 std::vector<std::unordered_set上使用 std::unique(<T>>