C++ 相互递归的变体类型

C++ Mutually Recursive Variant Type

本文关键字:类型 递归 C++      更新时间:2023-10-16

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

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

如您所见,Object类型是相互递归的,因为Array类型需要Map类型的声明,而类型的声明需要Array类型的声明。我怎样才能在 c++ 中表示这种类型?如果变体不是最好的方法,那是什么?

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

既然你使用的是boost::variant,那么使用它的递归包装器有什么问题?

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

它按预期工作。