I have the following union-like struct
struct MyStruct {
enum {A, B} tag;
union {
int option_1;
struct {
unordered_map<string, into> map;
int number;
}option_2;
};
MyStruct(int x) : tag(A) {
option_1 = x;
}
MyStruct(unordered_map<string, int> &map, int x) : tag(B) {
new (&option_2.map) unordered_map<string, int>(std::move(map));
option_2.number = x;
}
~MyStruct() {
// Some code here
}
};
The unordered_map
makes the union non trivial. Since I'm calling new
in one of the the constructors I would need to free the memory for the map once I'm done with the struct. So I thought I should call delete
in the destructor.
~MyStruct () {
if (tag == B) {
delete &option_2.map;
}
}
But this gives me runtime errors saying I can't call free on the pointer. I then simply tried to free the map by setting the other Union member, and it seemed to work (trying to access the map after the struct goes out of scope results in a seg fault).
~MyStruct () {
option_1 = 0;
}
My question is why doesn't my first attempt at a destructor work, and is the second attempt at a destructor the best practice here?