问题
I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following:
BooleanIntegerRealStringNameStreamArray<Object>Map<Object, Object>
As you can see, the Object type is mutually recursive because the Array type would require a declaration of the Map type which would require a declaration of the Array type. How could I go abouts representing this type in c++? If a variant isn't the best way, what is?
Here is what I have tried so far but it doesn't compile because of the requirements of std::unordered_map (I think) http://coliru.stacked-crooked.com/a/699082582e73376e
回答1:
Since you are using boost::variant, what is wrong about using its recursive wrappers ?
- recursive_variant
- recursive_wrapper
You can see a short example in the tutorial:
typedef boost::make_recursive_variant<
int
, std::vector< boost::recursive_variant_ >
>::type int_tree_t;
std::vector< int_tree_t > subresult;
subresult.push_back(3);
subresult.push_back(5);
std::vector< int_tree_t > result;
result.push_back(1);
result.push_back(subresult);
result.push_back(7);
int_tree_t var(result);
And it works as expected.
来源:https://stackoverflow.com/questions/19066946/c-mutually-recursive-variant-type