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 15:11:15 -0600
On Wed, 1 Mar 2023 at 14:48, 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.
>

Modulo arithmetic (aka wraparound) isn't the same thing as overflow.

And as for assigning negative numbers to unsigned types, whatever happened
> to type safety?
>

If an implicit conversion exists between types, the implication is that
it's safe to perform. There are some warts like narrowing and
std::string::string(char const*), but this usage is more manifestly safe
and correct than any concise alternative I'm aware of.

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
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-03-01 21:11:29