So I've seen it said before that to use unions in this manner is a bad idea. I know that it's technically undefined behavior. However, if I am using C++11 (and therefor only new-ish compilers) then honestly how bad could the following code really be? Is this actually likely to blow up on me? Could it be improved?
union registers_t
{
struct [[packed]]
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint8_t F, A, C, B, E, D, L, H, R, I;
#else
uint8_t A, F, B, C, D, E, H, L, I, R;
#endif
uint16_t SP, PC;
};
struct [[packed]]
{
uint16_t AF, BC, DE, HL;
};
};
Like I said, I know this is UB in C++ so there is no reason to point this out. My question is does that actually matter in this case.