C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 01 Mar 2023 16:19:52 -0800
On Wednesday, 1 March 2023 15:56:51 PST Lénárd Szolnoki via Std-Proposals
wrote:
> >I'm not gonna include a header file and use that horrible syntax.
> >Having an implicit variable called "__i" would be much better.
>
> What would it be called in an inner loop? __j?

And if you refactor and add an outer loop, would the variables get renamed?
That's a recipe for disaster.

If a counter variable is implicitly created, it ought to have the same name at
all levels, which means each level shadows the outer one. And you can't get
warnings like -Wshadow because you can't rename the variable.

I think we need a separate syntax for this, one that allows you to specify the
name of the variable. This syntax should also allow you to specify the initial
value and how to update, in case you want to count backwards or not in steps
of 1.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-03-02 00:19:54