Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost分配器无法在递归上下文中编译_C++_C++11_Boost_Allocator_Boost Interprocess - Fatal编程技术网

C++ boost分配器无法在递归上下文中编译

C++ boost分配器无法在递归上下文中编译,c++,c++11,boost,allocator,boost-interprocess,C++,C++11,Boost,Allocator,Boost Interprocess,下面我有一个我想要完成的完整示例。本质上,我希望我的树结构保存在内存映射中。下面的代码无法编译。我不理解(广泛的)错误;这是某种失败的类型转换。如何调整此代码以使其正常工作 #include "boost/interprocess/allocators/allocator.hpp" #include "boost/interprocess/managed_mapped_file.hpp" #include <map> #include <memory> templat

下面我有一个我想要完成的完整示例。本质上,我希望我的树结构保存在内存映射中。下面的代码无法编译。我不理解(广泛的)错误;这是某种失败的类型转换。如何调整此代码以使其正常工作

#include "boost/interprocess/allocators/allocator.hpp"
#include "boost/interprocess/managed_mapped_file.hpp"

#include <map>
#include <memory>

template <typename TKey, typename TData, template<class> class TAllocator = std::allocator>
class Node : std::enable_shared_from_this<Node<TKey, TData, TAllocator>> {
    using TNode = Node<TKey, TData, TAllocator>;
    std::map<TKey, TNode, std::less<TKey>, TAllocator<std::pair<const TKey, TData>>> _children;
    TData _data;
public:
    Node() = default;
    explicit Node(const TAllocator<std::pair<const TKey, TData>>& allocator, const TData& data) : _children(allocator), _data(data) {}

    TData& at() { return _data; }
    const std::map<TKey, std::shared_ptr<TNode>>& children() { return _children; };

    void add(const TKey& key, const TData& data) {
        _children.emplace(key, TNode(_children.get_allocator(), data));
    }
};

template <typename T>
using TAlloc = boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>;
using TMapTrie = Node<std::string, std::shared_ptr<std::size_t>, TAlloc>;

int main() {
    boost::interprocess::managed_mapped_file file_vec(boost::interprocess::open_or_create, "/tmp/pfx_mmap.dat", 1 << 20);
    TAlloc<std::pair<const std::string, std::shared_ptr<std::size_t>>> allocator(file_vec.get_segment_manager());

    TMapTrie root(allocator, nullptr);
    root.add("abc", std::make_shared<std::size_t>(42));
    return 0;
}

这应该是
libstdc++
中的
map
实现中的一个错误。它的map实现假设分配器的
指针
类型(即
typename std::allocator\u traits::pointer
)实际上是一个指针

这不是标准所要求的,而且
boost::interprocess
提供的分配器确实使用
offset\u ptr
作为
指针
类型,它是一个对象而不是一个实际指针

罪犯代码如下:

其中,
\u Link\u type
直接声明为指针:

  typedef _Rb_tree_node<_Val>*      _Link_type;

你知道这是否有一个现存的bug吗?@Brannon我还没查过。我想没有,否则它应该被修复。也许你可以把这个bug归档。@Brannon这很奇怪,这个问题实际上是在五年前提出来的,但他们仍然没有解决。。。也许他们认为这并不重要。在本例中,您可以使用内置的
map
。看见
  _Link_type
  _M_get_node()
  { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
  typedef _Rb_tree_node<_Val>*      _Link_type;
#include "boost/interprocess/allocators/allocator.hpp"
#include "boost/interprocess/managed_mapped_file.hpp"

#include <map>

template <typename T>
using TAlloc = boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>;

int main() {
    boost::interprocess::managed_mapped_file file_vec(boost::interprocess::open_or_create, "/tmp/pfx_mmap.dat", 1 << 20);
    TAlloc<std::pair<const int, int>> allocator(file_vec.get_segment_manager());

    std::map<int, int, std::less<int>, TAlloc<std::pair<const int, int>>> mp{allocator};
    mp.insert({1, 2});
}