C++ Logo


Advanced search

Subject: Re: [std-proposals] count-based for loop
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2019-08-27 11:54:23


On 27/08/2019 17:41, Justin Bassett via Std-Proposals wrote:
> When C++20's ranges comes, I'm planning on just:
> for (const auto i : std::views::iota(0, count)) {
>     ...
> }
> Which is remarkably similar to Python's for i in range(count). The
> naming is a bit unfortunate, though.
> Does this satisfy the need?

See also my blog post on this:

This provides

for(int x:range(count)){

> On Tue, Aug 27, 2019, 9:27 AM Sophia Poirier via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
> /_WHAT_/
> A count-based for loop provides a simple declarative means to
> declare within the loop's scope a zero-based counter variable of the
> same decayed type as an integral count value (satisfying the
> Integral concept) and will loop count number of times incrementing
> the counter variable by one after each iteration.
> for (const auto i : count)
> {
>     std::cout << i << '\n';
> }
> /_WHY_/
> As with range-based for loops, it reduces the typical boilerplate of
> a count-centered for loop and eliminates the the need to correctly
> declare and match types to the count value, leveraging auto
> deduction instead.  It provides a declarative means to iterate over
> a count rather than the imperative original for loop's three-piece
> construction.  It supports the C++ Core Guidelines P.3: Express
> intent
> <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rp-what>.
> While some might balk at a third for() overload, I believe this
> addition will actually relegate for(;;) to the esoteric/expert
> level, the same way that no-one really expects to see:
> for (std::/container/::iterator it = container.begin(); it !=
> container.end(); ++it)
> in new code anymore.
> Though it has been a couple of decades, I can still remember as a
> novice C++ programmer frequently slowing down on for loops, pausing
> to remember which operations exactly happen in each of the three
> expressions when and for which control flow purpose.  Are function
> calls in the middle expression executed for each iteration, or only
> the logical operation?  Will pre-increment vs post-increment cause a
> logical difference in the third expression? Will it make a
> performance difference?  Why am I seeing sign mismatch comparison
> warnings or implicit integer widening conversion warnings?  (Then
> noisy warnings usually just get disabled.)  Why did the counter
> overflow or underflow?  If I am decrementing the counter, will I
> want to test on ==0 or <=0 or <0?  These are uninteresting and
> burdensome mechanics for the typical idiomatic usage of simply
> counting up to N.
> std::ssize() http://wg21.link/P1227 attempts to solve this type of
> problem, but unfortunately in a rather incomplete fashion.  This
> problem exists far beyond STL containers and the size() function. 
> This proposal offers a more complete solution.
> Additionally, if [P1110] A placeholder with no name
> http://wg21.link/P1110%c2%a0is accepted, then there would be a simple way
> to create a loop to execute N times without a counter variable if it
> is unneeded, e.g.:
> for (__ : count)
> {
>     do_something();
> }
> thanks for your thoughts,
> Sophia
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups