2017-10-04 17 views
1

boost::unique_ptrstd::mapに格納しようとしています。私が使用しているコンパイラは、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 ::マップ内のブースト:: unique_ptrをを保存する方法はありますか?以前のバージョンでは許可されていますか?他の方法でこれを行うことはできますか?私はコンパイラを変更することはできません、私が作業しているコードは非常に古く、長い間変更されていません。

+0

古代コンパイラを使用する必要がある理由はありますか?そして、 'std :: unique_ptr'ではなく' boost :: unique_ptr'を使うでしょうか? –

+0

@TobySpeightコードベースは非常に古く、長い間変更されていません。私はコンパイラのアップグレードを提案しましたが、今のところそれを行う準備ができていません。 – TrailBlazer

+0

私はそれが事実かもしれないと思った。私はあなたの仕事を羨ましくない! –

答えて

2

value_typestd::mapには、insertのコピーコンストラクタが必要です。 boost::unique_ptrは、あなたは、単に(あなたもstd::moveboost::moveにそれを必要とします)emplaceを使用することができます(私が思うに削除されている可能性)のプライベートコピーコンストラクタ

を持っている:

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

しかし...あなたの場合std::map::emplaceを持っていれば、コンパイラはC++ 11をサポートしているので、boostの代わりにstd::unique_ptrを使うとよいでしょう。

P.S.

g ++ 3.4.6は+10歳で、最近のブーストバージョンでは何かを壊す可能性があります。時代遅れのコンパイラを使用する特別な理由はありますか?

+0

私は従来のコードベースで作業しています。彼らはまだC++ 11への移行を望んでいません。 – TrailBlazer

関連する問題