Date: Tue, 17 Nov 2026 15:17:46 +0100
wt., 17 lis 2026 o 14:18 David Brown via Std-Proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
>
> For loops are good when you are iterating over something fixed. They
> are not usually a good choice when changing the thing you are iterating
> over. Basically, if you ever feel you need to do something odd with the
> iterator that you are incrementing (or decrementing) - skipping the
> increment, repeating it, or otherwise changing its value - then "for" is
> unlikely to be the best choice of structure.
>
> auto i = l.begin();
> while (i != l.end()) {
> if (*i == value) {
> i = l.erase(i);
> } else {
> i++;
> }
> }
>
> (Disclaimer - not tested in any way.)
>
Or even better use some helper like:
```
eraseIf(l, [](auto& v) { return v == value; });
```
As it asks for bugs when you make loops like this too complex.
Question if we could create adaptors that could create sequences that
behave with some steps duplicated?
```
l = {0, 1, 2, 3, 4}
for (auto& [f, repeat] : specialOperation(l))
{
std::print("{} r: {}", f, repeat);
//some logic
}
```
and result is:
```
0 r: 0
1 r: 0
1 r: 1
2 r: 0
2 r: 1
2 r: 2
3 r: 0
3 r: 1
3 r: 2
3 r: 3
4 r: 0
4 r: 1
4 r: 2
4 r: 3
4 r: 4
```
>
> You only need multiple checks and nested loops if you try and force the
> use of a "for" loop where it does not fit the behaviour you want.
>
> David
>
>
>
>
>
> On 17/11/2026 13:53, Ell via Std-Proposals wrote:
> > If anything, I'd find it more useful to skip just the increment part of
> > a `for` loop, but still check the condition. Consider something like
> > `std::list::remove()`. If you wanted to express the repeated part using
> > a nested loop, you'd have to write something like:
> >
> > for (auto i = l.begin(); i != l.end(); ++i) {
> > while (i != l.end() && *i == value)
> > i = l.erase(i);
> >
> > if (i == l.end())
> > break;
> > }
> >
> > Look how many times you have to check that you're not accidentally
> > falling off the end. Instead, you could write:
> >
> > for (auto i = l.begin(); i != l.end(); ++i) {
> > if (*i == value) {
> > i = l.erase(i);
> > repeat;
> > }
> > }
> >
> > Of course, there are other ways to write this loop that may or may not
> > be more palatable, and either way I agree with everyone else that it's
> > too niche for a new keyword.
> >
> > On Monday, November 16th, 2026 at 6:41 PM, scbd Gm via Std-Proposals
> > <std-proposals_at_[hidden]> wrote:
> >
> >> Hello everyone,
> >>
> >> I'm interested in proposing a new C++ control-flow statement called
> >> `repeat`.
> >>
> >> The motivation is to provide a dedicated way to restart the current
> >> loop iteration immediately, without executing the remaining statements
> >> or the loop's increment expression.
> >>
> >> For example:
> >>
> >> int x = 0;
> >>
> >> for (int i = 0; i < 5; i++)
> >> {
> >> x++;
> >>
> >> cout << "Hello\n";
> >>
> >> if (x != 2)
> >> repeat;
> >> }
> >>
> >> My proposed semantics are:
> >>
> >> 1. Restart the innermost enclosing loop body.
> >>
> >> 2. Skip the remaining statements of the current iteration.
> >>
> >> 3. Do not execute the loop's increment expression.
> >>
> >> 4. Destroy and reinitialize local objects belonging to the loop
> >> body's scope.
> >>
> >> 5. Preserve variables declared outside the loop body.
> >>
> >> 6. Support `for`, `while`, and `do-while` loops.
> >>
> >>
> >> I believe this could provide a cleaner and more expressive alternative
> >> to certain uses of `goto` and nested loops.
> >>
> >> I would appreciate feedback on:
> >>
> >> - Whether this feature offers sufficient practical value.
> >>
> >> - Whether similar functionality has been proposed before.
> >>
> >> - Potential problems with object lifetimes, destructors, and
> >> exception handling.
> >>
> >> - Whether this idea could be worth developing into a formal C++
> >> proposal.
> >>
> >>
> >> This is an early-stage idea, and I would appreciate constructive
> >> criticism.
> >>
> >> Thank you!
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
>
> For loops are good when you are iterating over something fixed. They
> are not usually a good choice when changing the thing you are iterating
> over. Basically, if you ever feel you need to do something odd with the
> iterator that you are incrementing (or decrementing) - skipping the
> increment, repeating it, or otherwise changing its value - then "for" is
> unlikely to be the best choice of structure.
>
> auto i = l.begin();
> while (i != l.end()) {
> if (*i == value) {
> i = l.erase(i);
> } else {
> i++;
> }
> }
>
> (Disclaimer - not tested in any way.)
>
Or even better use some helper like:
```
eraseIf(l, [](auto& v) { return v == value; });
```
As it asks for bugs when you make loops like this too complex.
Question if we could create adaptors that could create sequences that
behave with some steps duplicated?
```
l = {0, 1, 2, 3, 4}
for (auto& [f, repeat] : specialOperation(l))
{
std::print("{} r: {}", f, repeat);
//some logic
}
```
and result is:
```
0 r: 0
1 r: 0
1 r: 1
2 r: 0
2 r: 1
2 r: 2
3 r: 0
3 r: 1
3 r: 2
3 r: 3
4 r: 0
4 r: 1
4 r: 2
4 r: 3
4 r: 4
```
>
> You only need multiple checks and nested loops if you try and force the
> use of a "for" loop where it does not fit the behaviour you want.
>
> David
>
>
>
>
>
> On 17/11/2026 13:53, Ell via Std-Proposals wrote:
> > If anything, I'd find it more useful to skip just the increment part of
> > a `for` loop, but still check the condition. Consider something like
> > `std::list::remove()`. If you wanted to express the repeated part using
> > a nested loop, you'd have to write something like:
> >
> > for (auto i = l.begin(); i != l.end(); ++i) {
> > while (i != l.end() && *i == value)
> > i = l.erase(i);
> >
> > if (i == l.end())
> > break;
> > }
> >
> > Look how many times you have to check that you're not accidentally
> > falling off the end. Instead, you could write:
> >
> > for (auto i = l.begin(); i != l.end(); ++i) {
> > if (*i == value) {
> > i = l.erase(i);
> > repeat;
> > }
> > }
> >
> > Of course, there are other ways to write this loop that may or may not
> > be more palatable, and either way I agree with everyone else that it's
> > too niche for a new keyword.
> >
> > On Monday, November 16th, 2026 at 6:41 PM, scbd Gm via Std-Proposals
> > <std-proposals_at_[hidden]> wrote:
> >
> >> Hello everyone,
> >>
> >> I'm interested in proposing a new C++ control-flow statement called
> >> `repeat`.
> >>
> >> The motivation is to provide a dedicated way to restart the current
> >> loop iteration immediately, without executing the remaining statements
> >> or the loop's increment expression.
> >>
> >> For example:
> >>
> >> int x = 0;
> >>
> >> for (int i = 0; i < 5; i++)
> >> {
> >> x++;
> >>
> >> cout << "Hello\n";
> >>
> >> if (x != 2)
> >> repeat;
> >> }
> >>
> >> My proposed semantics are:
> >>
> >> 1. Restart the innermost enclosing loop body.
> >>
> >> 2. Skip the remaining statements of the current iteration.
> >>
> >> 3. Do not execute the loop's increment expression.
> >>
> >> 4. Destroy and reinitialize local objects belonging to the loop
> >> body's scope.
> >>
> >> 5. Preserve variables declared outside the loop body.
> >>
> >> 6. Support `for`, `while`, and `do-while` loops.
> >>
> >>
> >> I believe this could provide a cleaner and more expressive alternative
> >> to certain uses of `goto` and nested loops.
> >>
> >> I would appreciate feedback on:
> >>
> >> - Whether this feature offers sufficient practical value.
> >>
> >> - Whether similar functionality has been proposed before.
> >>
> >> - Potential problems with object lifetimes, destructors, and
> >> exception handling.
> >>
> >> - Whether this idea could be worth developing into a formal C++
> >> proposal.
> >>
> >>
> >> This is an early-stage idea, and I would appreciate constructive
> >> criticism.
> >>
> >> Thank you!
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2026-11-17 14:18:02
