我有一个递归数据结构,我想存储在共享内存中。到目前为止,我有这个(简化):
using namespace boost::interprocess;
typedef allocator<wchar_t,
managed_shared_memory::segment_manager>TCharAllocator;
typedef basic_string<wchar_t, std::char_traits<wchar_t>, TCharAllocator> MyShmString;
class Node;
template<>
struct sizeof_value<Node>
{
static const std::size_t value = *NodeSize*; <---??
};
typedef private_node_allocator<Node, managed_shared_memory::segment_manager> TNodeAllocator;
class Node
{
private:
MyShmString _value;
vector<Node, TNodeAllocator> _children;
};
如果你为Nodesize填写一个数字,它会编译并运行。您可以通过编译和打印sizeof(节点)来获取此数字。这不是很强大。我已经尝试使用前向声明的constexpr函数返回Node的大小,但这不编译(在MSVC上)。还有另一种解决方法吗?
如果我理解正确,唯一的并发症来源是声明需要不完整类型的数据结构。
如果boost::interprocess::sizeof_value<>
专业化工作,这就足够了.¹
在稳健性方面,在static_assert
的定义完成后添加Node
很容易,以确保实际值与编译器报告的大小相匹配:
static_assert(boost::interprocess::sizeof_value<Node>::value == sizeof(Node), "sizeof mismatch");
¹我有预感可以在没有它的情况下完成,但我现在不能花时间让你的代码自成一体来检查