Boost 进程间 flat_map operator[] 编译错误

Boost interprocess flat_map operator[] compilation errors

我正在 boost::interprocess::flat_map 上构建一些包装器,问题是,由于某种原因我无法使用 operator[]at。当我使用 findinsert 时,它编译成功。

typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager;
typedef boost::interprocess::allocator<char, SegmentManager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>,
        CharAllocator> ShmString;
typedef short KeyType;
typedef ShmString MappedType;
typedef std::pair<const short, ShmString> ValueType;

typedef boost::interprocess::allocator<ValueType,
        boost::interprocess::managed_shared_memory::segment_manager> ShMemAlloc;
typedef boost::interprocess::flat_map<KeyType, MappedType,
        std::less<short>, ShMemAlloc> ShMap;


class Wrapper {

public:

    Wrapper(boost::interprocess::managed_shared_memory* memSeg) :
            m_memSeg(memSeg) {
        const ShMemAlloc initAlloc(m_memSeg->get_segment_manager());
        m_storage = m_memSeg->construct
            <ShMap> (boost::interprocess::anonymous_instance)
            (std::less<KeyType>(), initAlloc);
        ShmString str(initAlloc);
        ValueType val(10, str);

        m_storage->insert(val); //Ok

        ShMap::iterator it = m_storage->find(5); //Ok
        if(it != m_storage->end())
            it->second = str;

        (*m_storage)[5] = str; //Compilation error
    };
    ~Wrapper();

protected:
    boost::interprocess::managed_shared_memory* m_memSeg;

    ShMap* m_storage;
};

operator[] 调用中的分配器中的类型推导似乎有问题,但我不知道如何正确使用它。

这是错误报告:

../boost/include/boost/container/string.hpp: In instantiation of 'boost::container::container_detail::basic_string_base<Allocator>::members_holder::members_holder() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]':
../boost/include/boost/container/string.hpp:104:18:   required from 'boost::container::container_detail::basic_string_base<Allocator>::basic_string_base() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]'
../boost/include/boost/container/string.hpp:596:16:   required from 'boost::container::basic_string<CharT, Traits, Allocator>::basic_string() [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]'
../boost/include/boost/container/detail/value_init.hpp:31:13:   required from 'boost::container::container_detail::value_init<T>::value_init() [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >]'
../boost/include/boost/container/flat_map.hpp:846:52:   required from 'boost::container::flat_map<Key, T, Compare, Allocator>::mapped_type& boost::container::flat_map<Key, T, Compare, Allocator>::priv_subscript(const key_type&) [with Key = short int; T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Compare = std::less<short int>; Allocator = boost::interprocess::allocator<std::pair<const short int, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::container::flat_map<Key, T, Compare, Allocator>::mapped_type = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; boost::container::flat_map<Key, T, Compare, Allocator>::key_type = short int]'
../boost/include/boost/container/flat_map.hpp:469:4:   required from 'typename boost::enable_if_c<(((! boost::is_class<BOOST_MOVE_TEMPL_PARAM>::value) || (! boost::move_detail::is_rv<BOOST_MOVE_TEMPL_PARAM>::value)) && (! boost::is_same<Key, BOOST_MOVE_TEMPL_PARAM>::value)), T&>::type boost::container::flat_map<Key, T, Compare, Allocator>::operator[](const BOOST_MOVE_TEMPL_PARAM&) [with BOOST_MOVE_TEMPL_PARAM = int; Key = short int; T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Compare = std::less<short int>; Allocator = boost::interprocess::allocator<std::pair<const short int, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; typename boost::enable_if_c<(((! boost::is_class<BOOST_MOVE_TEMPL_PARAM>::value) || (! boost::move_detail::is_rv<BOOST_MOVE_TEMPL_PARAM>::value)) && (! boost::is_same<Key, BOOST_MOVE_TEMPL_PARAM>::value)), T&>::type = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >&]'
./include/.h:240:23:   required from here
../boost/include/boost/container/string.hpp:218:22: error: no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()'
../boost/include/boost/container/string.hpp: In instantiation of 'boost::container::container_detail::basic_string_base<Allocator>::members_holder::members_holder() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]':
../boost/include/boost/container/string.hpp:104:18:   required from 'boost::container::container_detail::basic_string_base<Allocator>::basic_string_base() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]'
../boost/include/boost/container/string.hpp:596:16:   required from 'boost::container::basic_string<CharT, Traits, Allocator>::basic_string() [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]'
../boost/include/boost/container/detail/value_init.hpp:31:13:   required from 'boost::container::container_detail::value_init<T>::value_init() [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >]'
../boost/include/boost/container/flat_map.hpp:846:52:   required from 'boost::container::flat_map<Key, T, Compare, Allocator>::mapped_type& boost::container::flat_map<Key, T, Compare, Allocator>::priv_subscript(const key_type&) [with Key = short int; T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Compare = std::less<short int>; Allocator = boost::interprocess::allocator<std::pair<const short int, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::container::flat_map<Key, T, Compare, Allocator>::mapped_type = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; boost::container::flat_map<Key, T, Compare, Allocator>::key_type = short int]'
../boost/include/boost/container/flat_map.hpp:469:4:   required from 'typename boost::enable_if_c<(((! boost::is_class<BOOST_MOVE_TEMPL_PARAM>::value) || (! boost::move_detail::is_rv<BOOST_MOVE_TEMPL_PARAM>::value)) && (! boost::is_same<Key, BOOST_MOVE_TEMPL_PARAM>::value)), T&>::type boost::container::flat_map<Key, T, Compare, Allocator>::operator[](const BOOST_MOVE_TEMPL_PARAM&) [with BOOST_MOVE_TEMPL_PARAM = int; Key = short int; T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Compare = std::less<short int>; Allocator = boost::interprocess::allocator<std::pair<const short int, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; typename boost::enable_if_c<(((! boost::is_class<BOOST_MOVE_TEMPL_PARAM>::value) || (! boost::move_detail::is_rv<BOOST_MOVE_TEMPL_PARAM>::value)) && (! boost::is_same<Key, BOOST_MOVE_TEMPL_PARAM>::value)), T&>::type = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >&]'
./include/.h:240:23:   required from here
../boost/include/boost/container/string.hpp:218:22: error: no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()'

这需要一些挖掘。您的问题是 flat_map::operator[] 要求映射类型 T 是默认可构造的,因为如果对象不存在,它需要能够在该位置插入默认对象。

您的ShmString不是默认可构造的,因为它没有默认可构造的分配器(它必须使用共享内存分配器)。

因此看来,在您的情况下,您将无法使用 operator[],而必须使用其他方法,例如 insertfind