Found another example where you would have to call destructor(s) manually. Suppose you have implemented a variant-like class that holds one of several types of data:
struct Variant {
union {
std::string str;
int num;
bool b;
};
enum Type { Str, Int, Bool } type;
};
If the Variant
instance was holding a std::string
, and now you're assigning a different type to the union, you must destruct the std::string
first. The compiler will not do that automatically.