Here is the definition:
struct nmap;
struct nmap: map<string, boost::variant<string, nmap*>>{};
The last line below doesn't work:
nmap my_map;
my_map["a"] = "b";
my_map["c"] = new nmap;
my_map["c"]["d"] = "e";
What do I need to add, in order for this to work?
I'd suggest either going for a tiny little readable helper:
Or go the recursive variant way. Let me sketch:
Recursive Variant Trees
This is IMO more elegant:
At first glance this may look more complicated, but it actually has a number of important advantages:
nmap
instances, there's full value-semantics out of the box.allows for idiomatic visiting of the tree, no need for 'ifs and
butsdereferences', e.g. consider how we could do a quick & dirty implementation of thatoperator<<
:See it Live On coliru
Output: