在std :: map

时间:2017-10-04 10:16:53

标签: c++ boost g++

我试图在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

有没有办法在std :: map中存储boost :: unique_ptr?在早期版本中是否允许?我们可以用其他方式做到这一点吗?我无法更改编译器,我正在处理的代码非常陈旧,并且很长时间没有被修改。

1 个答案:

答案 0 :(得分:2)

value_type的{​​p> std::map需要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岁,很可能在最近的版本中打破一些东西。有没有具体的理由使用过时的编译器?

相关问题