c++: variadic nested maps aka dictionary

197 views Asked by At

I'd like to have a container in cpp that kind of behaves like python dictionaries do, at least in the following regards:

  • key-value structure (key might be restricted to a single type)
  • variadic value type (might be restricted to some pod types[int, double, string])
  • deeply nested (arbitrary depth, but not necessary dynamic)
  • randomly accessed
  • accessed type is stored type

So at best, the following example should work;

typedef DictionaryContainer<string, <int, string, bool>, 2> Dict;
Dict mydict; //key-type is string, value-type one of int, string or bool, nested to depth 2
mydict["a"] = 1;
mydict["b"] = true;
mydict["c"] = 'string';
mydict["d"] = Dict();
mydict["d"]["a"] = false;

std::any& dict_d_a = mydict["d"]["a"]; // stores a bool
std::string& dict_c = mydict["c"]; // possibly get<std::string>(mydict["c"])

Is there anything like this out there? I have found containers that allow storing arbitrary types, like boost::variant but they do not allow random access and recursively defined containers (as far as I have figured it out).

Is there not some implementation of a tree / map that can be used in such way?

0

There are 0 answers