C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 1 Mar 2023 08:49:58 +0000
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.

Received on 2023-03-01 08:50:11