gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:27:30 25 4
gpt4 key购买 nike

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

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

你可以这样编译它:gcc demo.cpp -lboost_system -std=c++11 -lstdc++。编译错误:

cannot convert ‘std::allocator_traits<boost::interprocess::allocator<std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::pointer {aka boost::interprocess::offset_ptr<std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> > >, long int, long unsigned int, 0>}’ to ‘std::_Rb_tree<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> >, std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> > >, std::less<std::__cxx11::basic_string<char> >, boost::interprocess::allocator<std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::_Link_type {aka std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Node<std::__cxx11::basic_string<char>, std::shared_ptr<long unsigned int>, TAlloc> > >*}’ in return
{ return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }

最佳答案

这应该是 map 实现中的一个错误在 libstdc++ .它的映射实现假定分配器的 pointer类型(即 typename std::allocator_traits<some_allocator>::pointer )实际上是一个指针。

这不是标准所要求的,分配器由boost::interprocess提供确实使用了 offset_ptr作为pointer类型,它是一个对象而不是一个真正的指针。

bits/stl_tree.h 中的罪魁祸首代码:

  _Link_type
_M_get_node()
{ return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }

哪里_Link_type直接声明为指针:

  typedef _Rb_tree_node<_Val>*      _Link_type;

正确的实现应该使用 allocator_traits<...>::pointer检索指针类型。

libc++ ,GCC 和 Clang 都正确地接受了代码(您的原始代码有一些不相关的拼写错误......),并且它的实现确实符合上述方法。

一个最小的重现示例:

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

关于c++ - boost 分配器无法在递归上下文中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com