C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 28 Apr 2023 07:17:48 +0100
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, 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 06:18:00