【问题标题】:BOOST 1.73.0 Interprocess string allocator errorBOOST 1.73.0 进程间字符串分配器错误
【发布时间】:2021-05-25 18:13:46
【问题描述】:

我正在尝试使用 Boost 1.73.0 在共享内存中的自定义对象 SharedValue 中分配一个字符串

我的对象:

typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> char_allocator;

class SharedValue {
public:
  SharedValue(const char_allocator& ca);
  boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> val;
};

SharedValue::SharedValue(const char_allocator& ca) : val(ca) {}

然后在共享内存构建器中:

boost::interprocess::managed_shared_memory shm(boost::interprocess::open_or_create, "SharedMemory", 65536);
boost::interprocess::managed_shared_memory::allocator<char>::type ca(shm.get_allocator<char>());
auto *value = shm.find_or_construct<SharedValue>(string(ID + "_" + name + "_SharedValueSHM").c_str())(ca);

它给了我这个错误:

In file included from boost/include/boost/interprocess/containers/string.hpp:23,
                 from SharedValue.h:24,
                 from SharedValue.cpp:13:
boost/include/boost/container/string.hpp: In instantiation of ‘boost::container::dtl::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:100:18:   required from ‘boost::container::dtl::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:643: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> >]’
SharedValue.cpp:19:50:   required from here
boost/include/boost/container/string.hpp:220:27: 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()’
  220 |          : allocator_type()
      |                           ^
In file included from boost/include/boost/interprocess/segment_manager.hpp:38,
                 from boost/include/boost/interprocess/detail/managed_memory_impl.hpp:30,
                 from boost/include/boost/interprocess/managed_shared_memory.hpp:25,
                 from SharedValue.h:21,
                 from SharedValue.cpp:13:
boost/include/boost/interprocess/allocators/allocator.hpp:142:4: note: candidate: ‘template<class T2> boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T2, SegmentManager>&)’
  142 |    allocator(const allocator<T2, SegmentManager> &other)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:142:4: note:   template argument deduction/substitution failed:
In file included from boost/include/boost/interprocess/containers/string.hpp:23,
                 from SharedValue.h:24,
                 from SharedValue.cpp:13:
boost/include/boost/container/string.hpp:220:27: note:   candidate expects 1 argument, 0 provided
  220 |          : allocator_type()
      |                           ^
In file included from boost/include/boost/interprocess/segment_manager.hpp:38,
                 from boost/include/boost/interprocess/detail/managed_memory_impl.hpp:30,
                 from boost/include/boost/interprocess/managed_shared_memory.hpp:25,
                 from SharedValue.h:21,
                 from SharedValue.cpp:13:
boost/include/boost/interprocess/allocators/allocator.hpp:136:4: note: candidate: ‘boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char; SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>]’
  136 |    allocator(const allocator &other)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:136:4: note:   candidate expects 1 argument, 0 provided
boost/include/boost/interprocess/allocators/allocator.hpp:131:4: note: candidate: ‘boost::interprocess::allocator<T, SegmentManager>::allocator(boost::interprocess::allocator<T, SegmentManager>::segment_manager*) [with T = char; SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>; boost::interprocess::allocator<T, SegmentManager>::segment_manager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>]’
  131 |    allocator(segment_manager *segment_mngr)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:131:4: note:   candidate expects 1 argument, 0 provided

有人有解决这个错误的想法吗?

谢谢

【问题讨论】:

    标签: c++ c++11 boost memory-management boost-interprocess


    【解决方案1】:

    它在 1_73_0 上对我来说编译得很好:

    我可能不会那样写,但稍微简短一点:

    #include <boost/interprocess/containers/string.hpp>
    #include <boost/interprocess/managed_shared_memory.hpp>
    
    namespace bip = boost::interprocess;
    
    namespace Shared {
        using Mem = bip::managed_shared_memory;
        template <typename T>
            using Alloc = bip::allocator<T, Mem::segment_manager>;
    
        using String
            = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
    
        struct Value {
            Value(String::allocator_type a)
                : val(a) { }
            String val;
        };
    }
    
    int main()
    {
        using namespace Shared;
        Mem shm(bip::open_or_create, "SharedMemory", 65536);
    
        auto* value = shm.find_or_construct<Value>(
            "ID_name_SharedValueSHM")(shm.get_segment_manager());
    }
    

    这会回避你们之间明显的类型不匹配 char_allocator 和结果 boost::interprocess::managed_shared_memory::allocator&lt;char&gt;::type 。 真的,您可能应该检查它是否使用正确 在您的真实代码中拼写 typedef。

    利用来自段管理器指针的分配器的隐式构造。如果可以,请接受异构分配器,因为它们可以相互转换:

    template <typename Alloc>
    explicit Value(Alloc a) : val(a) { }
    

    这将在您变得更高级并使用例如范围分配器。

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2012-09-18
      相关资源
      最近更新 更多