C++ Logo


Advanced search

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

From: Gašper Ažman <gasper.azman_at_[hidden]>
Date: Tue, 7 Jan 2020 10:06:42 +0000
Did we inherit this from C?

On Tue, Jan 7, 2020 at 6:46 AM Владимир Прокофьев via Std-Proposals <
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
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-01-07 04:09:26