7/15/2026 at 1:45:21 AM
Reading this, I really can't make much sense of it: for (int i=0; auto&& it: vec)
cout << (++i) << ": " << it << endl;
It's certainly not obvious what's going on there at a glance.This is at least a bit more pythonic:
for (auto [i, it] : std::views::enumerate(vec)) {
std::cout << i << ": " << it << "\n";
}
by UncleOxidant
7/15/2026 at 2:33:59 AM
The part that confuses me is that they use “it” to name a variable that is not actually an iterator. That’s very bad style. I would name it “s”.by senkora
7/15/2026 at 8:34:22 AM
No argument that it's inappopriate, but I wonder if the author works with other programming languages - in some (Groovy, Kotlin, Ruby), "it" is the implicit block parameter.by pizza234
7/15/2026 at 4:35:12 PM
Haha I always interpreted "it" to mean literally the word "it". I didn't realize it's a short-hand form for iterator, but now I'm not so sure if that's universal or if there's a genuine split.by Maxatar
7/15/2026 at 8:55:33 AM
It's "it" as in the pronoun, not as in "iterator".by pwdisswordfishq
7/15/2026 at 11:18:31 AM
We call that “Larry Wall nomenclature”.by zbentley
7/15/2026 at 9:51:03 AM
As someone who wrote a lot of C and legacy C++, I find the first form easier to understand.The "int i=0; ... ++i" part is just your typical C-style loop. And the "auto&& it: vec" is the for-each kind of loop that dates back from C++11. The only new thing is that you can do both at the same time, which is not much of a stretch.
On the second one, I had to look up what the "std::views::enumerate" did. It is kind of obvious when you look at the code, but it is not as explicit.
And maybe most importantly, it doesn't do the same thing!
The first loop starts at one, you can tell because it is ++i, not i++. A debatable choice when it comes to readability, but you can see it right before your eyes. For the second loop, I had to, again, look up to make sure the result of "std::views::enumerate" was indeed zero-indexed (and it is, of course).
Which form you prefer depends on if you like being explicit or if you prefer abstractions. I usually prefer the former. Performance-wise, the generated code is probably very close, but the first one is likely to have an advantage on debug (unoptimized) builds.
by GuB-42
7/15/2026 at 1:47:56 AM
It's probably easier to understand if you read about the "if statement with initializer" linked from the post: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p03...by grg0
7/15/2026 at 1:58:44 AM
Not really, I was well aware of if-with-init and I still found this quite confusing. That one actually shows what the variable is initialized to. This one doesn't.by dataflow
7/15/2026 at 5:40:07 AM
> That one actually shows what the variable is initialized to. This one doesn't. replyWhich variable? `i` is clearly initialized to `0` and `it` doesn't have an initializer per se... it starts at the first value of the container.
It almost seems like an abuse to put 2 unrelated things in the `for(...)` but that's what they did.
by hdjrudni
7/15/2026 at 9:14:24 AM
It certainly is easy, for anyone that learnt other OOP languages like Smalltalk and Object Pascal before coming into C++, as I did back in the day.by pjmlp