在std :: map中插入boost :: unique_ptr

Inserting a boost::unique_ptr in std::map

本文关键字:unique boost ptr std map 插入      更新时间:2023-10-16

我正在尝试将boost::unique_ptr存储在std::map中。我正在使用的编译器是G 3.4(/usr/bin/g++34 -I./boost_1_62_0/ test_boost_unique_ptr.cpp)。

#include <iostream>
#include <map>
#include <boost/move/unique_ptr.hpp>
#include <boost/move/make_unique.hpp>
#include <boost/ptr_container/ptr_map.hpp>
using namespace boost::movelib;
typedef std::map<const int, unique_ptr<int> > MGR_T;
typedef MGR_T::const_iterator MGR_ITER_T;
MGR_T mgrPluginCache;
int main()
{
    unique_ptr<int> p (new int(10));
    std::cout << "p = " << *p << std::endl;
    mgrPluginCache.insert(std::make_pair(1, boost::move(p)));
//    mgrPluginCache[0] = (boost::move(p));
    return 0;
}

这会导致汇编错误:

In function `int main()':
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp:37: error:   initializing argument 2 of `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp: In destructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::~pair()':
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp: In copy constructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::pair(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)':
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp: In member function `void boost::ptr_map_adapter<T, VoidPtrMap, CloneAllocator, Ordered>::insert(const Range&) [with Range = std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >, T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >, VoidPtrMap = std::map<const int, void*, std::less<const int>, std::allocator<std::pair<const int, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered =  true]':
test_boost_unique_ptr.cpp:37:   instantiated from here
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `begin(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)'
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `end(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)'
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]':
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144:   instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
test_boost_unique_ptr.cpp:37:   instantiated from here
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144:   instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
test_boost_unique_ptr.cpp:37:   instantiated from here
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context

有没有办法将BOOST :: simel_ptr存储在std :: Map中?它可以在早期版本中吗?我们可以做其他方式吗?我无法更改编译器,我正在使用的代码非常旧,并且很长时间没有被修改。

std::mapvalue_type需要insert的复制构造函数。boost::unique_ptr有一个私有复制构造函数(我想可能已删除)

您可以简单地使用emplace(您还需要std::moveboost::move):

mgrPluginCache.emplace(1, boost::move(foo));

但是...如果您有std::map::emplace,则编译器支持C 11,那么您可以考虑使用std::unique_ptr而不是Boost。

p.s。

g 3.4.6已有 10年的历史,很可能会在最近的增强版中破坏一些东西。是否有使用过时的编译器的特定原因?