Reputation: 5073
Lately I have been studying C and I came across the concept of Union. I like it because it can be used to represent any data type. Of course it is helpful only for certain rare and special circumstances. In C++ everything is done with classes. Of course similar functionality like Union can be implemented using classes. So I wanted to know whether C++ supports a Union equivalent, so that I don't re-invent the wheel.
Upvotes: 1
Views: 847
Reputation: 279245
You probably want Boost.Variant
.
C++ does have union
like in C, but because a union
doesn't keep track of what type is stored in it, it wouldn't be able to execute the correct destructor if used with non-trivial types. The resulting restrictions on the use of union
in C++ make it quite non-general.
Upvotes: 5