M
Mark
Hello
still drilling the conversions topic, and I can't entirely conceive a few
paragraphs from the Standard. Let us consider a couple of cases:
(1)
char c;
int i;
c + i => We have two objects of integral types, so as per 6.3.1.8 integral
promotion results in (int)c + i. Following 6.3.1.8p1, what is applied
further:
"If both operands have the same type, then no further conversion is needed"
or
"Otherwise, if both operands have signed integer types or both have unsigned
integer types, the operand with the type of lesser integer conversion rank
is converted to the type of the operand with greater rank."
(1)a
int i;
short int j;
i + j => i + (int)j. The same question as in (1) applies for this case.
(2)
unsigned int i;
int j;
i + j => Promotion results in i + (unsigned int)j and then according to
6.3.1.8p1
"..if the operand that has unsigned integer type has rank greater or equal
to the rank of the type of the other operand, then the operand with signed
integer type is converted to the type of the operand with unsigned integer
type"
we have (unsigned int) i + (unsigned int) j. Seems to be correct to me?
(2)a
unsigned int i;
unsigned short int j;
i + j => i + (unsigned int)j and voila.
(3)
unsigned int u;
long int p;
u + p => this is a tricky case. I guess if the the size(long) == sizeof(int)
, integer promotion will end up u + (unsigned int)p. But what if
sizeof(long) > sizeof(int) ?
I think enough examples by now. Please correct my thoughts when wrong.
Thanks.
still drilling the conversions topic, and I can't entirely conceive a few
paragraphs from the Standard. Let us consider a couple of cases:
(1)
char c;
int i;
c + i => We have two objects of integral types, so as per 6.3.1.8 integral
promotion results in (int)c + i. Following 6.3.1.8p1, what is applied
further:
"If both operands have the same type, then no further conversion is needed"
or
"Otherwise, if both operands have signed integer types or both have unsigned
integer types, the operand with the type of lesser integer conversion rank
is converted to the type of the operand with greater rank."
(1)a
int i;
short int j;
i + j => i + (int)j. The same question as in (1) applies for this case.
(2)
unsigned int i;
int j;
i + j => Promotion results in i + (unsigned int)j and then according to
6.3.1.8p1
"..if the operand that has unsigned integer type has rank greater or equal
to the rank of the type of the other operand, then the operand with signed
integer type is converted to the type of the operand with unsigned integer
type"
we have (unsigned int) i + (unsigned int) j. Seems to be correct to me?
(2)a
unsigned int i;
unsigned short int j;
i + j => i + (unsigned int)j and voila.
(3)
unsigned int u;
long int p;
u + p => this is a tricky case. I guess if the the size(long) == sizeof(int)
, integer promotion will end up u + (unsigned int)p. But what if
sizeof(long) > sizeof(int) ?
I think enough examples by now. Please correct my thoughts when wrong.
Thanks.