使用`std::variant`进行递归`typedef`定义

huangapple go评论70阅读模式
英文:

Recursive typedef definition using std::variant

问题

我想定义一个可以存储一组字符串和值的std::variant,其中值是一组成对的值。

我想创建一个如下的结构:

typedef std::variant<bool, int, float, std::vector<std::pair<std::string, Value>>> Value;

在C++17中,我该如何实现这个?

英文:

I want to define a std::variant that can store a vector of pairs, with strings and Values.

I want to create a structure like below:

typedef std::variant&lt;bool, int, float, std::vector&lt;std::pair&lt;std::string, Value&gt;&gt;&gt; Value;

How can I do it in C++17?

答案1

得分: 3

正如HolyBlackCat在评论中提到的,您需要Value是一个类型(但可以是不完整的),才能在这个pair中使用它。

struct Value
{
    std::variant<bool, int, float, std::vector<std::pair<std::string, Value>>> data;
};

在coliru上查看

英文:

As HolyBlackCat notes in the comments, you need Value to be a type (but it can be incomplete) to use it in the pair.

struct Value
{
    std::variant&lt;bool, int, float, std::vector&lt;std::pair&lt;std::string, Value&gt;&gt;&gt; data;
};

See it on coliru

huangapple
  • 本文由 发表于 2023年1月9日 18:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定