C++ Logo

std-proposals

Advanced search

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

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Mon, 10 Feb 2025 22:31:39 +0000
On Mon, 10 Feb 2025, 21:42 Filip via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> I love the idea of simpler loops.
>
> ‘’’
> for (int i = 0; i < 42) { … } // run [0, 42)
> ‘’’
> In my opinion if the compiler knows what the limits are it should apply
> ++i or —i.
>
> ‘’’
> for (int i : 0 … 42 ) { … } // run [0, 42)
> ‘’’
> That version would be probably my preferred version since it could simply
> mean taking i from range literal from 0 up to but excluding 42.
> The range literal could be reused elsewhere.
>
> ‘’’
> for ( N ) { … } // run N amount of times
> ‘’’
> Many times I’ve needed to have a simple way to run N amount of times
> without specifying the loop iterator.
>

Are these loops less error prone? Faster?

Or are we really just worrying about saving a few keystrokes and typing
slightly less? That doesn't seem like a problem we need to focus on for the
future of C++.



>

Received on 2025-02-10 22:31:56