C++ Logo

std-proposals

Advanced search

Re: [std-proposals] for ( repeat_count )

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 23 Oct 2023 21:48:59 +0100
On Monday, October 23, 2023, Matthew Taylor via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I don't think it's particularly common that you will want a loop where you
> don't use an element of the range you're iterating over and you don't want
> to use the index; and that not only is a for loop the best tool for the job
> but also none of the alternatives which have been presented to you are a
> viable option.
>



I had proposed before that all 'for' loops should have an implicit variable
'__i' keeping count of the iterations, something like:

    for ( auto &e : my_vector ) SomeFunc( e, __i );

Of course it gets more complicated if you have nested loops, like in the
following:

    for ( auto &vec : vector_of_vectors )
    {
        SomeOtherFunc( e.Method(), __i + 1u );

        for ( auto &e : vec )
        {
            SomeFunc( e, __i );
        }
    }

but we could simply dictate that the '__i' refers to the counter for the
innermost loop. To make it more elaborate, the counters of outer loops
could be accessed from within the inner loopwith '__i[-1]' and '__i[-2]',
but the committee probably won't have an appetite for that.

Received on 2023-10-23 20:49:01