C++ Logo

std-proposals

Advanced search

count-based for loop

From: Sophia Poirier <spoirier_at_[hidden]>
Date: Tue, 27 Aug 2019 09:27:40 -0700
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.


RELATIONSHIP TO OTHER PROPOSALS

std::ssize() http://wg21.link/P1227 <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 <http://wg21.link/P1110> is 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

Received on 2019-08-27 11:29:50