C++ Logo

std-proposals

Advanced search

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

From: Ray Gardener <rgardener_at_[hidden]>
Date: Wed, 1 Mar 2023 08:32:41 -0800
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
>

Received on 2023-03-01 16:32:56