2017-10-04 1 views
1

J'essaie de stocker un boost::unique_ptr dans un std::map. Le compilateur que j'utilise est g ++ 3.4 (/usr/bin/g++34 -I./boost_1_62_0/ test_boost_unique_ptr.cpp).Insertion d'un boost :: unique_ptr dans std :: map

#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; 
} 

Il en résulte une erreur de compilation:

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 

Y at-il un moyen de stocker le boost :: unique_ptr dans std :: carte? Est-il autorisé dans les versions antérieures? Pouvons-nous faire cela d'une autre façon? Je ne peux pas changer le compilateur, le code sur lequel je travaille est très ancien et n'a pas été modifié depuis longtemps.

+0

Y a-t-il une bonne raison pour laquelle vous devez utiliser un ancien compilateur? Et 'boost :: unique_ptr' plutôt que' std :: unique_ptr'? –

+0

@TobySpeight La base de code est très ancienne et n'a pas été modifiée depuis longtemps. J'avais suggéré de mettre à jour le compilateur, mais ils ne sont pas prêts à le faire pour le moment. – TrailBlazer

+0

Je pensais que cela pourrait être le cas; Je n'envie pas votre tâche! –

Répondre

2

value_type de std::map nécessite un constructeur de copie pour insert. boost::unique_ptr ont un constructeur de copie privée (qui aurait pu être supprimé, je suppose)

Vous pouvez simplement utiliser emplace (vous aurez également besoin de std::move ou boost::move il):

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

Mais ... si vous avez std::map::emplace puis votre compilateur prend en charge C++ 11, alors vous pouvez envisager d'utiliser std::unique_ptr au lieu de boost.

P.S.

g ++ 3.4.6 est âgé de +10 ans et risque de casser quelque chose dans les versions de boost récentes. Y a-t-il une raison spécifique d'utiliser un compilateur obsolète?

+0

Je travaille sur un code de base hérité. Ils ne souhaitent pas passer à C++ 11 pour le moment. – TrailBlazer