Date: Mon, 10 Feb 2025 18:57:24 +0300
On 2/10/25 17:17, Pau Miquel Montequi Hernandez via Std-Proposals wrote:
> Greetings,
>
> I would like to discuss a revision of the for loop syntax, with the goal
> of evaluating possible improvements that could enhance its usability.
>
> *Common usage of for loops*
> The typical use case of a for loop is iterating over a collection of
> items when the number of iterations is known beforehand. More often than
> not, this means iterating one-by-one, yet the syntax requires explicitly
> specifying the increment operation:
>
> for (int i = 0; i != 42; ++i) // Most of the time, ++i is the
> expected behavior
>
>
> This raises the question: Can we simplify this pattern while keeping it
> expressive?
Quite honestly, looping with a counter is by far not the most common
kind of loops you write in real code. It is the simplest kind of loop
that they teach in classes, but in practice you most likely loop over
ranges or have an elaborate exit condition. So if you want to improve
syntax, you should propose something that will *drastically* improve a
wide variety of cases, primarily loops over ranges (including the
current ranged for form). I don't see your proposal doing this, sorry.
> *Alternative syntax inspired by range-for loops*
> A syntax resembling C++11's range-for loop can be considered:
>
> for (int i = 0 : 42) // Iterates from 0 to 42
> for (int id = get_first_id() : get_last_id())
> for (auto i = v.rbegin() : v.rend()) // Reverse iteration
I think, this can be achieved with std::ranges::subrange and a ranged
for loop.
> Greetings,
>
> I would like to discuss a revision of the for loop syntax, with the goal
> of evaluating possible improvements that could enhance its usability.
>
> *Common usage of for loops*
> The typical use case of a for loop is iterating over a collection of
> items when the number of iterations is known beforehand. More often than
> not, this means iterating one-by-one, yet the syntax requires explicitly
> specifying the increment operation:
>
> for (int i = 0; i != 42; ++i) // Most of the time, ++i is the
> expected behavior
>
>
> This raises the question: Can we simplify this pattern while keeping it
> expressive?
Quite honestly, looping with a counter is by far not the most common
kind of loops you write in real code. It is the simplest kind of loop
that they teach in classes, but in practice you most likely loop over
ranges or have an elaborate exit condition. So if you want to improve
syntax, you should propose something that will *drastically* improve a
wide variety of cases, primarily loops over ranges (including the
current ranged for form). I don't see your proposal doing this, sorry.
> *Alternative syntax inspired by range-for loops*
> A syntax resembling C++11's range-for loop can be considered:
>
> for (int i = 0 : 42) // Iterates from 0 to 42
> for (int id = get_first_id() : get_last_id())
> for (auto i = v.rbegin() : v.rend()) // Reverse iteration
I think, this can be achieved with std::ranges::subrange and a ranged
for loop.
Received on 2025-02-10 15:57:29