boost::interprocess контейнеры контейнеров и вектор push_back()

Я пытался изменить пример кода boost::interprocess для создания контейнеров контейнеров в общей памяти: http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/allocators_containers.html

Исходный код вставляет контейнер контейнеров в карту, но я бы хотел вставить его в вектор:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>

using namespace boost::interprocess;

//Typedefs of allocators and containers
typedef managed_shared_memory::segment_manager                       segment_manager_t;
typedef allocator<void, segment_manager_t>                           void_allocator;
typedef allocator<int, segment_manager_t>                            int_allocator;
typedef vector<int, int_allocator>                                   int_vector;
typedef allocator<int_vector, segment_manager_t>                     int_vector_allocator;
typedef vector<int_vector, int_vector_allocator>                     int_vector_vector;
typedef allocator<char, segment_manager_t>                           char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;

class complex_data
{
   int               id_;
   char_string       char_string_;
   int_vector_vector int_vector_vector_;

   public:
   //Since void_allocator is convertible to any other allocator<T>, we can simplify
   //the initialization taking just one allocator for all inner containers.
   complex_data(int id, const char *name, const void_allocator &void_alloc)
      : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
   {}
   //Other members...
};

//Definition of the map holding a string as key and complex_data as mapped type
typedef std::pair<const char_string, complex_data>                      map_value_type;
typedef std::pair<char_string, complex_data>                            movable_to_map_value_type;
typedef allocator<map_value_type, segment_manager_t>                    map_value_type_allocator;
typedef map< char_string, complex_data
           , std::less<char_string>, map_value_type_allocator>          complex_map_type;

typedef allocator<complex_data, segment_manager_t> complex_data_allocator;
typedef vector<complex_data, complex_data_allocator> complex_vector_type;

int main ()
{
   shared_memory_object::remove("MySharedMemory");
   remove_shared_memory_on_destroy remove_on_destroy("MySharedMemory");
   {
      //Create shared memory
      managed_shared_memory segment(create_only,"MySharedMemory", 65536);

      //An allocator convertible to any allocator<T, segment_manager_t> type
      void_allocator alloc_inst (segment.get_segment_manager());

      //Construct the shared memory map and fill it
      complex_map_type *mymap = segment.construct<complex_map_type>
         //(object name), (first ctor parameter, second ctor parameter)
            ("MyMap")(std::less<char_string>(), alloc_inst);

      complex_vector_type *myvec = segment.construct<complex_vector_type>
            ("MyVec")(alloc_inst);

      for(int i = 0; i < 100; ++i){
         //Both key(string) and value(complex_data) need an allocator in their constructors
         char_string  key_object(alloc_inst);
         complex_data mapped_object(i, "default_name", alloc_inst);
         map_value_type value(key_object, mapped_object);
         //Modify values and insert them in the map
         mymap->insert(value);
         myvec->push_back(mapped_object);
      }
   }
   return 0;
}

Призыв к myvec->push_back(mapped_object) вызывает следующую ошибку компиляции:

In file included from ipc.cpp:30:
In file included from /usr/local/Cellar/boost/1.56.0/include/boost/interprocess/containers/vector.hpp:19:
In file included from /usr/local/Cellar/boost/1.56.0/include/boost/container/vector.hpp:50:
/usr/local/Cellar/boost/1.56.0/include/boost/container/detail/advanced_insert_int.hpp:147:10: error: no viable
      overloaded '='
      *p =v_;
      ~~ ^~~
/usr/local/Cellar/boost/1.56.0/include/boost/container/vector.hpp:2306:29: note: in instantiation of member function
      'boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> >, complex_data *>::copy_n_and_update' requested here
         insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
                            ^
/usr/local/Cellar/boost/1.56.0/include/boost/container/vector.hpp:2017:19: note: in instantiation of function template
      specialization 'boost::container::vector<complex_data, boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> >
      >::priv_forward_range_insert_expand_forward<boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> >, complex_data *> >' requested here
            this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
                  ^
/usr/local/Cellar/boost/1.56.0/include/boost/container/vector.hpp:1885:16: note: in instantiation of function template
      specialization 'boost::container::vector<complex_data, boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> >
      >::priv_forward_range_insert_no_capacity<boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> >, complex_data *> >' requested here
         this->priv_forward_range_insert_no_capacity
               ^
/usr/local/Cellar/boost/1.56.0/include/boost/container/vector.hpp:1424:58: note: in instantiation of function template
      specialization 'boost::container::vector<complex_data, boost::interprocess::allocator<complex_data,
      boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> > >::priv_push_back<complex_data>' requested here
   BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
                                                         ^
/usr/local/Cellar/boost/1.56.0/include/boost/move/detail/move_helpers.hpp:54:14: note: expanded from macro
      'BOOST_MOVE_CONVERSION_AWARE_CATCH'
   {  return FWD_FUNCTION(const_cast<const TYPE &>(x)); }\
             ^
ipc.cpp:96:17: note: in instantiation of member function 'boost::container::vector<complex_data,
      boost::interprocess::allocator<complex_data, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void,
      long, unsigned long, 0>, 0>, iset_index> > >::push_back' requested here
         myvec->push_back(mapped_object);
                ^
ipc.cpp:45:7: note: candidate function (the implicit copy assignment operator) not viable: 1st argument
      ('const value_type' (aka 'const complex_data')) would lose const qualifier
class complex_data
      ^
1 error generated.

Предположительно это потому, что myvec->push_back(mapped_object) пытается сделать копию объекта (чего не происходит с картой). Я предполагаю, что проблема связана с тем, что char_string_ а также int_vector_vector_ нужно инициализировать с void_allocator, чего не происходит в конструкторе копирования по умолчанию. Я пытался сохранить ссылку на void_allocator в complex_dataи написал свой собственный конструктор копирования, который использует его для инициализации скопированного объекта, но я все еще получаю похожие ошибки.

Может кто-нибудь сказать мне, где я иду не так? Благодарю.

0 ответов

Другие вопросы по тегам