C++ boost::进程间::托管共享内存中的std::list

C++ boost::进程间::托管共享内存中的std::list,c++,boost,stl,allocator,boost-interprocess,C++,Boost,Stl,Allocator,Boost Interprocess,最近,我学习了在boost::进程间::托管共享内存段中使用无序映射的正确方法。到目前为止还不错,但我还需要添加一些STL容器 理想情况下,我希望能够遵循任何STL容器的相同要求。现在我需要一份工作。我做不到。不过,我可以做一件工作 向量 以下代码起作用: #include <vector> #include <boost/interprocess/managed_shared_memory.hpp> namespace ipc = boost::interproces

最近,我学习了在
boost::进程间::托管共享内存
段中使用
无序映射的正确方法。到目前为止还不错,但我还需要添加一些STL容器

理想情况下,我希望能够遵循任何STL容器的相同要求。现在我需要一份工作。我做不到。不过,我可以做一件工作

向量 以下代码起作用:

#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);
}
#包括
#包括
名称空间ipc=boost::进程间;
使用段=ipc::托管共享内存;
使用Manager=Segment::Segment\u Manager;
使用Alloc=ipc::分配器的模板;
使用Vector=std::Vector的模板;
int main(){
boost::进程间::共享内存\对象::删除(“测试”);

段_段{ipc::仅创建_,“测试”,1ul并非所有标准库实现都完全支持有状态分配器(还?)

在这种情况下,您的
std::list
似乎没有。只需选择一个用于Boost容器的容器,它也可以通过Boost进程间标题方便地获得:

#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);
}
#包括
#包括
名称空间ipc=boost::进程间;
使用段=ipc::托管共享内存;
使用Manager=Segment::Segment\u Manager;
使用Alloc=ipc::分配器的模板;
使用列表的模板=ipc::列表;
int main(){
boost::进程间::共享内存\对象::删除(“测试”);
段_段{ipc::仅创建_,“测试”,1ul
第9段:

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

您的分配器可以拒绝为除T之外的任何对象分配内存。这将阻止它在基于节点的容器中使用,例如std::list,它需要分配自己的内部节点类型(不仅仅是容器的值类型),但对于std::vector它可以正常工作


正如@Jonathan Wakely所发布的,这有点讽刺,正如昨天对你的
std::unordered_map
答案的评论。这个解决方案有效——正确编译和插入。没问题。感谢你发布另一个自我回答的问题,刚才投票支持:)他们是否全面索赔?如果他们为一个容器索赔但不是另一个,它不是一个bug。也许有一个“功能准备就绪”页面使承诺更加明确。他们跳过“未测试”功能不是很合理吗?顺便说一下,另一个常见的障碍是使用非原始指针类型(所以当
分配器::指针类型!=T*
)正如上面的评论所说,问题不是缺少对有状态分配器的支持,而是缺少对“奇特指针”的支持,而且已经有一个bug报告:但这不是问题所在。
$ 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.
#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);
}