Let's consider a union of integers of different sizes. Is it guaranteed that if a number fits the range of each of the integer types, it can be written to and read out from any of the union data members correctly?
E.g. this code
union U {
int32_t i;
int16_t s;
} u;
u.s = 1000;
std::cout<<u.i<<std::endl;
I verified that it prints correctly "1000" on one computer. Is it guaranteed to work the same on any other system? I guess on any system the endianness would be the same for any integer type, so it's rather a question whether the union is guaranteed to use the less significant bytes of the larger integer for the smaller one?
I know this has no chance to work for negative numbers, so let's consider non-negative numbers only.