2017-10-04 130 views
1

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

#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 ::地图了boost ::的unique_ptr的方法吗?在早期版本中允许吗?我们能否以其他方式做到这一点?我无法更改编译器,我正在处理的代码很旧,并且很久没有修改过了。

+0

是否有很好的理由需要使用古代编译器? 'boost :: unique_ptr'而不是'std :: unique_ptr'? –

+0

@TobySpeight代码库很旧,并且很久没有修改过。我曾建议升级编译器,但他们还没有准备好现在就做。 – TrailBlazer

+0

我认为可能是这样;我不羡慕你的任务! –

回答

2

value_type of 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岁,很可能会在最近的升级版本中出现问题。是否有任何特定的原因使用过时的编译器?

+0

我正在研究遗留代码库。他们不希望移动到C++ 11呢。 – TrailBlazer

相关问题