std::string not working in anonymous union

This code here:

1
2
3
4
5
6
7
8
9
enum variableType {real, str};
struct variable
{
  enum variableType type;
  union{
    double realValue;
    std::string stringValue;
  };
};

makes these errors:
1
2
3
member `std::string variable::<anonymous union>::stringValue' with constructor not allowed in union
member `std::string variable::<anonymous union>::stringValue' with destructor not allowed in union
member `std::string variable::<anonymous union>::stringValue' with copy assignment operator not allowed in union 


can somebody explain why please?
Last edited on
Unlike C, C++ doesn't allow non-basic types in unions. You'll have to make it an std::string * and create the object dynamically.
Unlike C, C++ doesn't allow non-basic types in unions.

C doesn't have non-POD types...
Last edited on
Topic archived. No new replies allowed.