C++ Logo

std-proposals

Advanced search

Re: [std-proposals] for-loops revision (simplification and new syntax)

From: Simon Schröder <dr.simon.schroeder_at_[hidden]>
Date: Fri, 14 Feb 2025 06:51:50 +0100
Some of the discussion now steered towards: “Is a new loop syntax useful?”

I would prefer a syntax that leverages the existing range syntax (something similar to Rust or Swift). There is probably one case where such a new syntax would help avoid bugs:
for(std::size_t i = vec.size()-1; i >= 0; --i) …
i.e. counting down a loop with an unsigned integer. This might be obfuscated a little more if one uses ‘auto’ to declare the loop counter. With a new syntax (that also allows to specify the step size and the step size can be negative) the implementation would hopefully fix this edge case.

> On Feb 13, 2025, at 10:15 PM, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> 
> One should add that the else branch is not run in Python, when the loop has been left with a corresponding break instruction.
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: Magnus Fromreide via Std-Proposals <std-proposals_at_[hidden]>
>
> If we are talking of loop Pythonisms then a loop else branch does add some
> value as that case often is a bit contrieved to test for. (The else branch
> of a loop is executed when the loop condition fails to run the loop more
> times)
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-02-14 05:52:05