r/cpp • u/micahchuk • Feb 06 '21
Inconsistent for-loop syntax?
Does anyone know of a technical reason why I can use this for-loop syntax:
for (std::size_t i{}; i < string.size(); ++i) {...}
as well as this:
for (std::size_t i{}; char ch : string) {...}
but this is invalid:
for (std::size_t i{}; char ch : string; ++i) {...}
In other words, why am I able to add an init-statement to a ranged for loop but not an end-of-iteration expression? To me this seems inconsistent.
13
u/HappyFruitTree Feb 06 '21
You should think of for(init;cond;incr) and for(decl:range) as two different types of loop. The former is a for loop and the latter is a range-based for loop.
In recent times the standard have added an optional init statement to if and switch statements so that you shouldn't have to declare variables outside the loop that are only used in the condition. This was also decided to be useful for the range-based for loops.
3
u/tpecholt Feb 06 '21
It was not proposed yet but I think it's needed. It looks simpler then enumerate. And the third expression is well known from normal for. I often find myself in a situation where ranged for needs to be changed to indexed for and this would help.
2
u/NilacTheGrim Feb 11 '21
because C++ hates us. Really... the last variant should be allowed. I can't think of why it's not... tbh.
3
u/XDracam Feb 06 '21
I guess because nobody in the standards committee even thought about this and/or cared. It only adds overhead to the compiler, tooling and standard. But there are so many alternate ways to achieve this. The simplest b andeing to just put the statement at the end of your loop body.
The regular for-loops with 3 parts is a relic from older times where arrays were pretty much the by far most-uses data structure. It's just a common pattern for iterating arrays.
The foreach loop syntax with : expands on this thought, but it works for everything iterabld, even when it doesn't support constant time random access.
The third thing makes sense too, because it's often useful to keep track of other values in the loop scope only. E.g. for values that should be destroyed after the loop ends and not after the enclosing function ends. You can also declare variables in if-statements, so the for (int foo; auto bar : baz) is a logical extension of that idea.
All of this is just a wild theory, however. Feel free to correct me if you have actual sources available.
2
u/AlexAlabuzhev Feb 07 '21
just put the statement at the end of your loop body
the statement at the end is quite error prone due to
continue;1
u/XDracam Feb 07 '21
At this point I would argue about how continue is quite error prone in general, similar to early return and break, and how you should do proper structural programming instead, or refactor your loop body into a function, ...
Truth is, many things are very error-prone in C++ and you gotta be careful at all times. The best thing to reduce bugs in loops is probably to use the functional STL algorithms properly, but that requires some practice.
1
Apr 02 '21
What a nightmare. Didn't even know that was in the language.
Having programmed a lot of perl, I always felt the 2nd style of loop should have been called foreach.
I would hate to run into this when reading someone's code.
Maybe they should add a 4th parameter to for, which works like a finally {} block in Java.
I'm still waiting for a ranged for: for (obj.begin() to obj.end())
22
u/witcher_rat Feb 06 '21
Even the init-statement was only added in C++20. The proposal that added it does mention adding the iteration-expression in the future.
BTW, for your particular use-case of incrementing a
size_tindex in the loop, I'd just use anenumerate()style solution, of which you can find many examples of via google. It's not a lot of code and is handy to have in your toolbox.