Let's say I have this code that has UB:
union Flag {
constexpr Flag() : empty{} {}
struct {} empty;
bool value;
};
static Flag flag;
int main() {
return flag.value;
}
where the UB is accessing value when it’s not the active member of the union Flag.
Currently, UBSan will not catch this error because (as I understand it) UBSan does not have a way to check the last written member of a union. For this particular case, I think UBSan could catch some UB going on here indirectly via the same check for non-true/false values for type bool. If the byte of an empty struct is considered “uninitialized” or “can have any arbitrary value”, then the compiler could legally set the arbitrary byte of this empty struct to to any non-zero/one value, and then UBSan would be able to catch the load of an invalid value for bool.
What I'd like to know is: Is it semantically permissible to have the nominal byte of empty structs–and more generally, any padding bytes in all structs–be initialized to any non-zero pattern?