C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Discussion: A proposed repeat statement for C++

From: Ell <ell_se_at_[hidden]>
Date: Tue, 17 Nov 2026 19:50:54 +0200
On 11/17/26 17:32, David Brown wrote:
>
> I agree that these things are open to interpretation, and "arguable".
> But I disagree in my interpretation :-)
>
> Key features of a "for" loop, as I see it, are that the structure you
> are looping over is not changed, the iterator progresses continuously,
> and that the iterator is not changed other than by the third clause of
> the "for" statement. Then you know exactly how the loop is looping by
> looking at the "for" statement itself, rather than the contents of the
> loop. (It's okay to "break" out of the loop early.)
>
> C++ does not enforce these restrictions, but I think they are useful
> guides for clear code. They follow a general pattern of changing things
> in one place only, which makes it easier to know what is going on and
> where variables are modified. (Modern programming languages often
> default to immutable - const - variables unless you specifically choose
> to make them mutable, precisely because it makes the logic of the code
> clearer.)
>
> To my highly subjective (obviously) eyes, the "while" loop accurately
> represents the logic of the code, but your nested "for" and "while" (or
> "for" with "repeat") seems needlessly convoluted.
>

The `while` version is not logically (or philosophically) wrong, but
that's not saying much. `while` is just a very generic loop construct
(second only to rust's `loop` maybe, which forgoes even the loop
condition). It doesn't constrain the loop the way a `for` does, but it
also doesn't tell you much about what kind of structure to expect.

I'm not sure I fully agree about a `for` loop not changing the structure
of the sequence at all. You don't want to change the structure in a way
that affects the logical flow of the loop, but I wouldn't say that's
what's happening here. Logically, you either eliminate the current
element or not, but the loop is still contiguous. If you wanted to
*insert* an element before each occurrence of `value`, which doesn't run
into the same technical problem, would you not use a `for` loop? Input
iterators can also arguably change the structure of the sequence as you
go along.

I do agree that you generally don't want to mess with the the iterator
in the body of the loop, though. It's down to personal judgement whether
violating this in specific cases makes the code more readable or
logical. Arguably, even `break` can be a bit of a blemish. I do know I
*have* seen people write stuff like `--i; continue;`, whether or not it
was a good idea.

> Marcin's "eraseIf" has merits too, but IMHO it complicates a rather
> simple task.
>

I mean, we already have `list::remove[_if]()`, and even a free
`erase[_if]()` that forwards to it. That was just an example.

> Anyway, the point is that there are various ways to write this kind of
> thing in relatively clear and simple code without having to invent a new
> "repeat" statement.
>

Like I said, I also think it's too niche. I'm not actually proposing it.

Received on 2026-11-17 17:53:00