jeudi 27 décembre 2018

boost allocator fails to compile in recursive context

I have a full sample of what I want to accomplish below. Essentially I wanted my tree structure held in a memory map. The code below doesn't compile. I don't understand the (expansive) error; it's some kind of failed type conversion. How do I adjust this code to make it work?

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

You can compile it like this: gcc demo.cpp -lboost_system -std=c++11 -lstdc++. The compilation error:

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

Aucun commentaire:

Enregistrer un commentaire