std :: list in boost :: interprocess :: managed_shared_memory

时间:2018-08-16 19:06:15

标签: c++ boost stl allocator boost-interprocess

最近我got schooled并学习了在unordered_map段中包含boost::interprocess::managed_shared_memory的正确方法。到目前为止一切顺利,但是我将需要添加更多STL容器。

理想情况下,我希望能够对任何STL容器都遵循相同的规则。现在,我需要一个std::list。我不能使它工作。不过,我可以进行std::vector的工作。

std :: vector

以下代码有效:

#include <vector>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using Vector = std::vector<K, Alloc<K>>;

int main() {
  boost::interprocess::shared_memory_object::remove("test");
  Segment _segment{ipc::create_only, "test", 1ul<<40};
  Manager *mgr = _segment.get_segment_manager();
  Vector<int> *v = _segment.construct<Vector<int>>("v")(mgr);
  v->emplace_back(1);
}

std :: list

等效于列表的代码会导致错误。

#include <list>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using List = std::list<K, Alloc<K>>;

int main() {
  boost::interprocess::shared_memory_object::remove("test");
  Segment _segment{ipc::create_only, "test", 1ul<<40};
  Manager *mgr = _segment.get_segment_manager();
  List<int> *v = _segment.construct<List<int>>("v")(mgr);
  v->emplace_back(1);
}

以下是编译标志(使用g ++-7)和错误:

$ g++ -std=gnu++17 lol_list.cpp -lrt -pthread -rdynamic -Wfatal-errors && ./a.out && rm ./a.out
In file included from /usr/include/c++/7/list:63:0,
                 from lol_list.cpp:1:
/usr/include/c++/7/bits/stl_list.h: In instantiation of ‘std::__cxx11::list<_Tp, _Alloc>::_Node* std::__cxx11::list<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::_Node = std::_List_node<int>]’:
/usr/include/c++/7/bits/stl_list.h:1801:32:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::_M_insert(std::__cxx11::list<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]’
/usr/include/c++/7/bits/stl_list.h:1133:4:   required from ‘std::__cxx11::list<_Tp, _Alloc>::reference std::__cxx11::list<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::reference = int&]’
lol_list.cpp:16:20:   required from here
/usr/include/c++/7/bits/stl_list.h:578:11: error: cannot convert ‘boost::interprocess::offset_ptr<std::_List_node<int>, long int, long unsigned int, 0>’ to ‘std::__cxx11::list<int, boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::_Node* {aka std::_List_node<int>*}’ in return
    return __p;
           ^~~
compilation terminated due to -Wfatal-errors.

使用g++-6g++-8甚至使用clang-6.0clang-5.0时,错误仍然存​​在。

2 个答案:

答案 0 :(得分:2)

并非所有标准库实现都完全支持有状态分配器(还可以吗?)。

在这种情况下,您的std::list<>似乎没有。只需选择一个用于Boost容器的容器,也可以通过Boost Interprocess头方便地使用它:

#include <boost/interprocess/containers/list.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using List = ipc::list<K, Alloc<K>>;

int main() {
    boost::interprocess::shared_memory_object::remove("test");
    Segment _segment{ipc::create_only, "test", 1ul<<40};
    Manager *mgr = _segment.get_segment_manager();
    List<int> *v = _segment.construct<List<int>>("v")(mgr);
    v->emplace_back(1);
}

答案 1 :(得分:1)

  

[allocator.requirements]第9段:

     
    

分配器可以约束可实例化它的类型以及可以为其构造成员调用的参数。如果类型不能与特定的分配器一起使用,则分配器类或构造调用可能无法实例化。

  
     

您的分配器可以拒绝为T以外的任何东西分配内存,这是可以的。这将防止它在基于节点的容器(例如std :: list)中使用,这些容器需要分配自己的内部节点类型(而不仅仅是容器的value_type),但对于std :: vector

效果很好

@ Jonathan-Wakely在https://stackoverflow.com/a/28995278/4288486中发布