C++ Logo

std-proposals

Advanced search

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

From: Federico Kircheis <federico_at_[hidden]>
Date: Wed, 01 Mar 2023 16:51:39 +0000
On 1 March 2023 16:32:41 UTC, Ray Gardener via Std-Proposals <std-proposals_at_[hidden]> wrote:
>size_t can be assigned -1?
>
>On Wed, Mar 1, 2023 at 12:50 AM Frederick Virchanza Gotham via
>Std-Proposals <std-proposals_at_[hidden]> wrote:
>
>> Sometimes I write code like this:
>>
>> size_t i = -1;
>> for ( auto const &e : some_container )
>> {
>> ++i;
>> DoSomeProcessing(e, i);
>> }
>>
>> It would be nice if range-based for loops had an implicit counter
>> variable, so that the above code snippet could simply be written as:
>>
>> for ( auto const &e : some_container )
>> {
>> DoSomeProcessing(e, __i);
>> }
>>
>> I propose that this would work even with containers that don't allow
>> random access.
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>

Why not?

size_t has a value, that added to 1, gives 0 as result

Received on 2023-03-01 16:51:45