Consider the following class, which can either be in a "non-empty" or "empty" state, and in the "empty" state the other member is default initialized (hence has an indeterminate value):
struct MaybeInt {
bool has_value;
int value;
MaybeInt() : has_value(false) {}
MaybeInt(int v) : has_value(true ), value(v) {}
};
Is it allowed to assign from a default-constructed MaybeInt
, as in:
MaybeInt empty, another;
another = empty; // OK?
How about construction?
MaybeInt empty, another(empty); // OK?
Does the answer change if MaybeInt::value
has type char
?