C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Reduce undefined behavior of signed integer literal arithmetic operations

From: Bo Persson <bo_at_[hidden]>
Date: Fri, 28 Apr 2023 09:41:55 +0200
On 2023-04-28 at 08:17, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Apr 27, 2023 at 9:55 PM Julien Villemure-Fr├ęchette via
> Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Doesn't work that way. Signed arithmetic overflow is UB to permit compilers to transform, for instance
>> ```
>> auto a = -(-b);
>> // to...
>> auto a = b;
>> ```
>> For all values of `b` for which the expression `-(-b)` is well defined.
>
>
> If that's the only reason why signed overflow is UB,

It's not.

No wrap around also lets the compiler assume that a < a + 1 is always
true. Useful for loop conditions.


> then the solution is very simple:
>
> Negation of numeric_limits<T>::min() shall yield numeric_limits<T>::min()
>
> This is what happens on all Two's Complement CPU's anyway.

Received on 2023-04-28 07:42:05