C ++ Mutually recursive variant type

I am trying to represent a PDF object type in C ++ using options. The PDF object is one of the following:

  • Boolean
  • Integer
  • Real
  • String
  • Name
  • Stream
  • Array<Object>
  • Map<Object, Object>

As you can see, the Object type is mutually recursive, because the Array type requires a Map type declaration, which requires an Array type declaration. How could I get around introducing this type in C ++? If the option is not the best, then what?

Here is what I have tried so far, but it does not compile due to the requirements of std::unordered_map (I think) http://coliru.stacked-crooked.com/a/699082582e73376e

+3
source share
1 answer

Since you are using boost::variant , what is wrong with using its recursive wrappers?

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.

+5
source

Source: https://habr.com/ru/post/1271065/


All Articles