J
Jens Thoms Toerring
Roman Mashak said:I came across this method of checking the sizes:
static union
{
char int8_t_incorrect[sizeof( int8_t) == 1];
char uint8_t_incorrect[sizeof( uint8_t) == 1];
char int16_t_incorrect[sizeof( int16_t) == 2];
char uint16_t_incorrect[sizeof(uint16_t) == 2];
char int32_t_incorrect[sizeof( int32_t) == 4];
char uint32_t_incorrect[sizeof(uint32_t) == 4];
It was recommended to use this declaration in order to let compiler "report
typedef errors automatically". I'm not sure I clearly understand what the
above statement does. It's my understanding that if intN_t/uintN_t types
exist on a platfrom, 'sizeof' operator returns a proper size of them.... and
then what?
I guess it's meant to provoke the compiler to emit an error
message (or at least a warning) in the case that int8_ta and
uint8_t don't have a size of 1, int8_t/uint8_t don't have a
size of 2 etc. since in that case e.g. 'sizeof( int8_t ) == 1'
woukd be false and thus result in the value 0. And then you
would try to create an array of length 0 which isn't legal.
I don't know if this is a good idea, I guess I would instead
use something that tests if CHAR_BITS is 8 since that seems
to be what all that boils down to.
Regards, Jens