【发布时间】:2017-06-14 20:01:29
【问题描述】:
我正在将一些旧代码移至 c++14,它使用了已弃用的 auto_ptr,并且与 boost:ptr_map 配合得很好,你可以这样做:
auto_ptr<Foo> foo(new Foo);
boost:map_ptr<int, Foo> m;
m.insert(5, foo);
现在,将 auto_ptr 替换为 unique_ptr,它不会编译:
unique_ptr<Foo> foo(new Foo);
boost:map_ptr<int, Foo> m;
m.insert(5, foo); // Does not compile
m.insert(5, move(foo)); // Does not compile either,
// this should be the right thing to do
m.insert(5, move.release()); // Does compile, but isn't exception safe
map_ptr API 还不是最新的吗?
根据响应进行编辑,在我的情况下,使用 unique_ptr 的映射不是一个好选择,因为它需要重写大量代码。我真的很想让它与 map_ptr 一起工作,我正在处理一些旧代码,我希望做出最少的更改。
【问题讨论】:
-
什么是 boost ptr_map?您确定在 C++14 中仍然需要它吗?我不认为你这样做,那些旧的指针容器是为了解决 auto_ptr 实际上不能由容器一般处理的事实。
-
为什么不直接使用
std::map<int, std::unique_ptr<Foo>>?