如何解决BOOST 1.73.0 进程间字符串分配器错误
我正在尝试使用 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::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::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::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.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 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::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::iset_index>; boost::interprocess::allocator<T,SegmentManager>::segment_manager = boost::interprocess::segment_manager<char,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
谢谢
解决方法
它在 1_73_0 上编译得很好:
- Live On Wandbox
- 在编译器资源管理器(GCC、MSVC)上直播:https://godbolt.org/z/15TM9c
我可能不会那样写,但稍微简短一些:
#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<char>::type
。
真的,你应该检查它是否正确使用
在您的实际代码中拼写 typedef。
利用段管理器指针隐式构造分配器。如果可以,接受异构分配器,因为它们可以相互转换:
template <typename Alloc>
explicit Value(Alloc a) : val(a) { }
当您变得更高级并使用例如时,这将有所帮助范围分配器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。