std::string not working in anonymous union

Feb 1, 2010 at 9:34am
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 Feb 1, 2010 at 9:35am
Feb 1, 2010 at 10:09am
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.
Feb 1, 2010 at 11:06am
Unlike C, C++ doesn't allow non-basic types in unions.

C doesn't have non-POD types...
Last edited on Feb 1, 2010 at 11:06am
Topic archived. No new replies allowed.