If you cast references between two integers of different sizes, say using reinterpret_cast, you start sinking into dangerous undefined behavior, right?
uint16_t originalInt = 5000; uint8_t &smallerThanOriginalRef = reinterpret_cast<uint8_t&>(originalInt); uint32_t &largerThanOriginalRef = reinterpret_cast<uint32_t&>(originalInt);
Would assigning to 'smallerThanOriginalRef' only set the first byte of 'originalInt'?
smallerThanOriginalRef = 123;
Wouldn't first clear the higher bytes of 'originalInt', and so 'originalInt' wouldn't be guaranteed to be 123, right?
And assigning to 'largerThanOriginalRef' would accidentally write on bytes outside of 'originalInt', which might write on memory that is used by other variables?
(in practice, those ints might be internally represented as a 32 bit or 64 bit integer anyway... but that's not guaranteed by the standard)