C++ Logo


Advanced search

Re: Mistake in C++ Standard Arithmetic Conversion makes C++ compiler unreliable

From: Michael Hava <mfh_at_[hidden]>
Date: Tue, 7 Jan 2020 17:52:37 +0000
Yes, this is inherited from C: https://en.cppreference.com/w/c/language/conversion
Otherwise (the signedness is different): If the unsigned type has conversion rank greater than or equal to the rank of the signed type, then the operand with the signed type is implicitly converted to the unsigned type.

I don’t have an up to date copy of the C standard lying around, but in N1570 it was specified in § to convert to the unsigned type if it has a greater or equal rank…


From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Gašper Ažman via Std-Proposals
Sent: Tuesday, January 7, 2020 11:07 AM
To: std-proposals_at_[hidden]
Cc: Gašper Ažman <gasper.azman_at_[hidden]>; Владимир Прокофьев <vov-crao_at_[hidden]>
Subject: Re: [std-proposals] Mistake in C++ Standard Arithmetic Conversion makes C++ compiler unreliable

Did we inherit this from C?

On Tue, Jan 7, 2020 at 6:46 AM Владимир Прокофьев via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:

I have found a mistake in the C++ Standard, https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversion : in the rule:

Otherwise, if the unsigned operand's conversion rank is greater or equal to the conversion rank of the signed operand, the signed operand is converted to the unsigned operand's type.

The NEGATIVE signed value CANNOT be converted to unsigned for any types. But opposite conversion from unsigned to signed is valid with appropriate rank.

To prove, please see wrong results here https://rextester.com/CKKDX46498

Note, that the error is observed for VS, CLang and GCC C++ compilers.

As you can see the error is observer in division operation to unsigned 32 and 64 bits.
The division operation to SIGNED types is VALID (see first table in the example).
Also multiplication of signed and unsigned values is VALID.

The example above shows that the C++ compiler shows unreliable behavior which leads to produce wrong values as a result of division.

The correct rule shall be:

Otherwise, unsigned operand is converted to signed type of same or higher conversion rank of unsigned operand's type and then the operand with lesser conversion rank is converted to the operand with the greater conversion rank

С уважением.

Std-Proposals mailing list

Received on 2020-01-07 11:55:10