C++ Mutually Recursive Variant Type

断了今生、忘了曾经 提交于 2020-01-14 10:48:29

问题


I am trying to represent a PDF object type in c++ using variants. A 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 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!