c++ - boost::unordered_map 使用 boost::interprocess::cached_node_allocator 编译失败

标签 c++ boost compiler-errors allocator boost-interprocess

我正在尝试在 boost::interprocess::managed_shared_memory 段中创建一个 boost::unordered_map。这工作正常,直到我尝试从使用 boost::interprocess::allocator 更改为 boost::interprocess::cached_node_allocator。

似乎 hash_bucket 结构推迟了键大小的计算,但 cached_node_allocator 在编译时需要大小。关于如何使这项工作有任何想法吗?

示例代码:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/segment_manager.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/cached_node_allocator.hpp>
#include <boost/unordered_map.hpp>

using namespace boost::interprocess;

int main ()
{
  //Create shared memory.
  managed_shared_memory segment(create_only, 
                                "SharedMem",  //segment name
                                65536);

  // Define cached node allocator type for char in a shared memory segment.
  typedef boost::interprocess::
      cached_node_allocator<char,
      managed_shared_memory::segment_manager> cached_node_allocator_t;

  // Define an interprocess string type which uses the cached node allocator.
  typedef boost::interprocess::
      basic_string<char, std::char_traits<char>,
      cached_node_allocator_t> shared_string_t;

  // Get an instance of the allocator.
  cached_node_allocator_t allocator_instance(
      segment.get_segment_manager());

  // Create an unordered map with Key and Value as interprocess strings,
  // using the default hash and predicate and use the cached node allocator.
  boost::unordered_map<shared_string_t, shared_string_t,
                       boost::hash<shared_string_t>, 
                       std::equal_to<shared_string_t>,
                       cached_node_allocator_t> my_map_t(allocator_instance);

  return 0;
}

海湾合作委员会输出:

$ g++ -I/opt/local/include cnasum.cc
/opt/local/include/boost/interprocess/allocators/detail/allocator_common.hpp: In instantiation of ‘const size_t boost::interprocess::sizeof_value<boost::unordered_detail::hash_bucket<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >::value’:
/opt/local/include/boost/interprocess/allocators/cached_node_allocator.hpp:104:   instantiated from ‘boost::interprocess::cached_node_allocator<boost::unordered_detail::hash_bucket<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul>’
/opt/local/include/boost/unordered/detail/fwd.hpp:134:   instantiated from ‘boost::unordered_detail::hash_bucket<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >’
/opt/local/include/boost/unordered/detail/fwd.hpp:278:   instantiated from ‘boost::unordered_detail::hash_iterator_base<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul>, boost::unordered_detail::ungrouped>’
/opt/local/include/boost/unordered/detail/fwd.hpp:347:   instantiated from ‘boost::unordered_detail::hash_buckets<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul>, boost::unordered_detail::ungrouped>’
/opt/local/include/boost/unordered/detail/fwd.hpp:921:   instantiated from ‘boost::unordered_detail::types<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::hash<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, std::equal_to<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul>, boost::unordered_detail::map_extractor<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > > >, boost::unordered_detail::ungrouped>’
/opt/local/include/boost/unordered/detail/unique.hpp:125:   instantiated from ‘boost::unordered_detail::map<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::hash<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, std::equal_to<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >’
/opt/local/include/boost/unordered/unordered_map.hpp:63:   instantiated from ‘boost::unordered_map<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::hash<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, std::equal_to<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >’
cnasum.cc:35:   instantiated from here
/opt/local/include/boost/interprocess/allocators/detail/allocator_common.hpp:43: error: invalid application of ‘sizeof’ to incomplete type ‘boost::unordered_detail::hash_bucket<boost::interprocess::cached_node_allocator<std::pair<const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::cached_node_allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, 64ul> >’ 
$

最佳答案

unordered_map 的构造函数必须采用几个参数:

typedef boost::unordered_map< ... > map_type;

map_type m( 100,                              // number of buckets
            boost::hash<shared_string_t>(),   // hasher object, pass temporary by value
            std::equal_to<shared_string_t>(), // predicate object, ditto
            allocator_instance                // your allocator object
          );

关于c++ - boost::unordered_map 使用 boost::interprocess::cached_node_allocator 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7423208/

相关文章:

c++ - 在不同波形格式之间转换 (WAVEFORMATEX)

C++ 抽象类析构函数

c++ - 对于 boost::variant,是否有 boost::visit 之类的 std::visit?

iphone - 这段代码有什么错误?

c++ - 在 C++ 中动态创建堆

c++ - 是否有任何现有库在 C/CPP/Obj-C 中实现了 matlab 函数?

c++ - 如何在异步Boost中只读取一包数据?

c++ - Boost::python 使用和返回模板公开 C++ 函数

c++ - 尝试urldownloadtofile函数时出错

ios - 转换为 Swift 3 错误 : "Convert to Current Swift Syntax Failed Could not find test host for..."