vendredi 5 janvier 2018

Recursive using declaration with boost variant

I am trying to represent a tree-like recursive data structure where each node may be one of two different data types. I employ the boost variant to "house" the two types that might be present at each node.

However, I run into a problem. I'm declaring all these types strictly with 'using' directives, so when I get to the recursive nature of a node, it fails since typedef/using may not use recursion.

How to accomplish this?

using LeafData = int; // just for illustration
using LeafNode = std::unordered_map<std::string, LeafData>;
using InnerNode = std::unordered_map<std::string, boost_variant<InnerNode, LeafNode>>; // this is problematic since I'm using InnerNode recursively

I have explored using the boost::make_recursive_variant but the type it creates (A) is not quite what I need, as that makes a variant within a variant when instead I want a (B) single variant consisting of either an InnerNode or LeafNode.

(A) boost_variant<boost_variant<InnerNode, LeafNode>, LeafNode>
(B) boost_variant<InnerNode, LeafNode>

Aucun commentaire:

Enregistrer un commentaire