C ++相互递归变体类型 [英] C++ Mutually Recursive Variant Type

查看:97
本文介绍了C ++相互递归变体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用变体在c ++中表示PDF对象类型。 PDF对象是以下对象之一:

I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following:


  • 布尔值

  • 整数

  • Real

  • 字符串

  • 名称

  • Stream

  • Array< Object>

  • Map< Object,Object>

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

如您所见, Object 类型是相互递归的,因为 Array 类型需要声明 Map 类型,这需要声明 Array 类型。我怎么能在C ++中代表这种类型呢?如果不是最佳方法,那是什么呢?

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?

这是我到目前为止尝试过的方法,但是由于<$ c的要求而无法编译$ c> std :: unordered_map (我认为) http:// coliru .stacked-crooked.com / a / 699082582e73376e

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

推荐答案

由于您使用的是 boost :: variant ,使用其递归包装有什么问题?

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

  • recursive_variant
  • recursive_wrapper

您可以在教程

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);

它可以按预期工作。

这篇关于C ++相互递归变体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆