2

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Stuart
  • 25
  • 4

1 Answers1

3

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

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • In my code base the Value is used as std::variant in other places. Is there a way that I can hold the Value type as variant? Thanks for the help. – Stuart Jan 09 '23 at 11:27
  • Extended example: https://godbolt.org/z/d95eq1YqW – Marek R Jan 09 '23 at 11:34
  • 3
    Here is version where `Value` is still a `std::variant`: https://godbolt.org/z/abTKasbo1 this should better fit into your existing code base. – Marek R Jan 09 '23 at 11:40