Boost.Pointer Container made obsolete by std::unique_ptr in C++11/14?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:50:47

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.

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

It's not obslete; it has a completely different and more intuitive interface than std::vector<std::unique_ptr<T>>.

try to use 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

Boost.Pointer Container certainly has more intuitive interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!