C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Array Index in Range-based For Loops

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Wed, 1 Mar 2023 10:48:04 -0600
Integral conversion to unsigned has always been well defined. In C++03 it
was [conv.integral]/2:

> If the destination type is unsigned, the resulting value is the least
unsigned integer congruent to the source integer (modulo 2n where n is the
number of bits used to represent the unsigned type). [ Note: In a two’s
complement representation, this conversion is conceptual and there is no
change in the bit pattern (if there is no truncation). — end note ]

The change in C++20 made the antecedent to the Note vacuously true,
implying that the consequent always holds. But that's irrelevant here,
because -1 was a literal and so the bit pattern didn't matter.


On Wed, 1 Mar 2023 at 10:39, Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, Mar 1, 2023 at 11:32 AM Ray Gardener via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > size_t can be assigned -1?
>
> Since C++20, signed integers are two's complement, and the conversion
> from signed to unsigned for negative numbers is done in a two's
> complement way. So yes, this is now well-defined.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-03-01 16:48:19