C++ Logo

std-proposals

Advanced search

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

From: Federico Kircheis <federico_at_[hidden]>
Date: Thu, 02 Mar 2023 06:06:12 +0000
On 1 March 2023 20:47:58 UTC, Ray Gardener via Std-Proposals <std-proposals_at_[hidden]> wrote:
>Not gonna lie, if I see code exploiting integer overflow, I reject it. It
>just smells hoity-toity. I like code to be dead obvious.
>
>And as for assigning negative numbers to unsigned types, whatever happened
>to type safety?
>
>Ray
>
>
>On Wed, Mar 1, 2023 at 11:21 AM Thiago Macieira via Std-Proposals <
>std-proposals_at_[hidden]> wrote:
>
>> On Wednesday, 1 March 2023 08:32:41 PST Ray Gardener via Std-Proposals
>> wrote:
>> > size_t can be assigned -1?
>>
>> Sure it can. Any out-of-range value is simply stored as if repeatedly
>> adding
>> or subtracting (SIZE_MAX + 1) until it is in range.
>>
>> so -1 is (SIZE_MAX + 1) - 1, which is SIZE_MAX.
>>
>> Some compilers will print a warning though (ICC was notable for the
>> "change of
>> sign" warning).
>>
>> --
>> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>> Software Architect - Intel DCAI Cloud Engineering
>>
>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>

Which overflow?

Lucky us, using -1 for denoting the inverse of 1 when doing an addition is the same syntax used in math and all other fields that use numbers (yes, also for numbers with modulo arithmetic, like in this case)

Received on 2023-03-02 06:06:19