Search code examples
c++c++17std-variant

Recursive typedef definition using std::variant


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<bool, int, float, std::vector<std::pair<std::string, Value>>> Value;

How can I do it in C++17?


Solution

  • 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<bool, int, float, std::vector<std::pair<std::string, Value>>> data;
    };
    

    See it on coliru