Home > Enterprise >  Is std::vector allowed inside std::variant union?
Is std::vector allowed inside std::variant union?

Time:10-27

I'm using std::variant as a safe-type union. On https://en.cppreference.com/w/cpp/utility/variant I've read that

A variant is not permitted to hold references, arrays, or the type void.

Does it mean that std::vector isn't allowed to be hold inside std::variant? - it is kind of array, yet I can hold std::string inside my variant, what probably I could not do using simple c union.
I couldn't find direct answer for that (dummy) question.

CodePudding user response:

Does it mean that std::vector isn't allowed to be hold inside std::variant?

It does not mean that. std::vector is none of references, arrays, or the type void. It is a (template for a) class type. std::variant is allowed to hold std::vector.

it is kind of array

The abstract data structure that std::vector models is a kind of an "array". But nevertheless, it isn't an array type.

yet I can hold std::string inside my variant

std::string also models an abstract data structure that is kind of an "array". In fact, the data structure is nearly identical to vector in many regards. But indeed, neither template is an array and both of them can be held in a variant.

  •  Tags:  
  • c
  • Related