解决方案 1 - 使用包装类:
struct node {
using myvar = boost::variant< unsigned, boost::recursive_wrapper< node > >;
using childmap = std::unordered_map< std::string, myvar >;
node() {}
node( std::initializer_list< childmap::value_type > il ) :
children( il ) {}
childmap children;
};
我在这里使用 boost::variant,因为我没有可用的 std::variant。
boost::recursive_wrapper 是必需的,因为 boost::variant 通常需要完整的类型,但此时 node 仍然不完整。
boost::recursive_wrapper 没什么神奇的。它只是一个指针的包装器!众所周知,可以为不完整类型声明指针而不会出现问题。这个包装类只是通过处理分配、释放和提供值语义来隐藏使用指针的事实。它有 boost::variant 的特殊支持,使包装器完全透明,因此可以像根本没有包装器类一样使用该变体。
用法:
node n {
{ "foo", 1 },
{ "bar", 2 },
{ "baz", node {
{ "bim", 3 },
{ "bam", 4 }}
}
};
n.children[ "fum" ] = 5;
n.children[ "fup" ] = node{{ "fap", 6 }};
初始化器列表中的显式“节点”是必需的,因为变体构造器无法从嵌套的初始化器列表中推断出类型。
演示:http://coliru.stacked-crooked.com/a/123c59a3523c39ed
解决方案 2 - 源自 unordered_map:
这消除了对“children”成员的需要。
struct nodemap :
std::unordered_map<
std::string,
boost::variant< unsigned, boost::recursive_wrapper< nodemap > > >
{
using base = std::unordered_map<
std::string,
boost::variant< unsigned, boost::recursive_wrapper< nodemap > > >;
// Forward all constructors of the base class.
using base::base;
};
用法:
nodemap n{
{ "foo", 1 },
{ "bar", 2 },
{ "baz", nodemap{
{ "bim", 3 },
{ "bam", 4 }}
}};
n[ "fum" ] = 5;
n[ "fup" ] = nodemap{{ "fap", 6 }};
更多使用示例:
// Add something to a child nodemap.
boost::get<nodemap>( n[ "baz" ] )[ "fap" ] = 7;
// This will throw a boost::bad_get exception because n[ "foo" ] is not a nodemap.
//boost::get<nodemap>( n[ "foo" ] )[ "fap" ] = 8;
// To avoid this problem, we can check if the child actually is a nodemap:
if( nodemap* pn = boost::get<nodemap>( &n[ "foo" ] ) )
{
(*pn)[ "fap" ] = 8;
}
演示:http://coliru.stacked-crooked.com/a/69914ec5646129f2