While this code is compiled without any error, I doubt that it will work as expected. Is such nesting allowed? I cannot use boost and c++17.
class Node;
typedef struct Value {
ValueTag type;
union {
std::int32_t integerValue;
std::float_t floatValue;
bool boolValue;
std::vector<Node> arrayValue;
std::unordered_map<std::string, Node> dictionaryValue;
};
} Value;
class Node {
private:
Value m_value;
public:
virtual ~Node();
};
Edit My original answer was incorrect/misleading, plz ignore it. for the reason see the comments below. conside std::variant or boost::variant anyway :)
Non PlainOldData(POD) are unsafe in c-unions.
Especially for this proplem std::variant came into the c++17 standard. You could use boost::variant for earlier versions of C++.
The problem is, that union does not know anything about constructors and destructors. Therefore it is not possible to do more general things with elements of a union, that would be necessary (e.g. dynamic memory allocation)