Underlying type balancing for integral operands

Moderators: david ward, misra cpp

Post Reply
abgs
Posts: 12
Joined: Wed Sep 06, 2017 4:25 pm
Company: Gimpel Software

Underlying type balancing for integral operands

Post by abgs » Fri Oct 06, 2017 4:21 pm

From "Underlying type balancing" on page 57:
Otherwise, if both operands have integral type, the underlying type of the expression can be found using the following:
– If the types of the operands are the same size, and either is unsigned, the result is unsigned.
– Otherwise, the type of the result is that of the larger type.
1. Does "the types of the operands" refer to underlying type or actual type?

2. Does "and either is unsigned" mean "and either is an unsigned integral type" or "and either is the type unsigned [int]"?

3. What is the intended meaning of "the result is unsigned" in the first bullet? A literal reading might suggest that the result is simply unsigned, i.e. the type unsigned int. I do not think this is the intended behavior. I assume it was meant to have the effect as if it said "the result is the [underlying?] type of the unsigned operand(s)".

4. Does "that of the larger type" refer to underlying type or actual type?

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Underlying type balancing for integral operands

Post by misra cpp » Wed Jan 17, 2018 4:33 pm

To answer your questions:

1) underlying type

2) "and either is an unsigned integral type"

3) "the result is the actual type of the unsigned operand(s)"

4) underlying type
Posted by and on behalf of
the MISRA C++ Working Group

abgs
Posts: 12
Joined: Wed Sep 06, 2017 4:25 pm
Company: Gimpel Software

Re: Underlying type balancing for integral operands

Post by abgs » Wed Feb 21, 2018 8:57 pm

What is the intended interaction between 1 and 3, particularly the transition from underlying type to actual type for the "unsigned operand"? This seems to lead to strange outcomes for code like a_u16 + b_u16 + c_u16 where there are expressions whose underlying type is unsigned while the actual type is signed. The only way I see to interpret 1 and 3 together as written would be as "if the essential type of either operand is unsigned, the result is the actual type of the essentially unsigned operand". Presumably either "either is unsigned" should refer to actual type, or "the result is unsigned" should refer to the essential type of the unsigned operand. The latter seems to produce the most consistent results when applied to the rules in 6.5.

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Underlying type balancing for integral operands

Post by misra cpp » Tue Apr 17, 2018 1:13 pm

The wording of our previous post could have been clearer -
for 3, its the underlying type of the result that is unsigned
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.5 Expressions (C++)”