C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Mistake in C++ Standard Arithmetic Conversion makes C++ compiler unreliable
From: Владимир Прокофьев (vov-crao_at_[hidden])
Date: 2020-01-07 00:46:14


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 list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups