Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

1–10 of 89 posts

Re: C++20 Improved the For-Loop Syntax

#3
The C++20 version is still clearly inferior to the Python and Lua examples because you still have to manually increment the counter in the loop body. IMO the sibling comment by HarHarVeryFunny has a much better C++ equivalent for this idiom, even if it's slightly more verbose.

Re: C++20 Improved the For-Loop Syntax

#5

Just what C++ needs, more bloat lol.

C++ is probably the only language that needs a strong style guide, to define a subset you are allowed to use in the project.

Linus Torwalds famously said that subset is zero. You're not allowed to use C++ in Linux, a wise move.

Here's Google's: https://google.github.io/styleguide/cppguide.html Search for "do not use" and you'll find plenty of hits.

Re: C++20 Improved the For-Loop Syntax

#6
The way this website shows the programming languages is odd. They're blue and slanted and if you hover your mouse cursor over them they have a color transition, so you'd think they are links - and yet you click on them and nothing happens

Re: C++20 Improved the For-Loop Syntax

#7
post #3

The C++20 version is still clearly inferior to the Python and Lua examples because you still have to manually increment the counter in the loop body. IMO the sibling comment by HarHarVeryFunny has a much better C++ equivalent for this idiom, even if it's slightly more verbose.

> you still have to manually increment the counter in the loop body

It doesn't look like that to me, the ++i thing seems to be just to start printing the array from 1 (I don't know how things are in Python nowadays but I know in Lua arrays start at 1, so there's no need for something like this in there), the value of i is still increasing without telling it explicitly to do so

Re: C++20 Improved the For-Loop Syntax

#8
post #3

The C++20 version is still clearly inferior to the Python and Lua examples because you still have to manually increment the counter in the loop body. IMO the sibling comment by HarHarVeryFunny has a much better C++ equivalent for this idiom, even if it's slightly more verbose.

> you still have to manually increment the counter in the loop body It doesn't look like that to me, the ++i thing seems to be just to start printing the array from 1 (I don't know how things are in Python nowadays but I know in Lua arrays start at 1, so there's no need for something like this in there), the value of i is still increasing without telling it explicitly to do so

Python is 0-indexed. In OP's example, the start parameter makes i start at 1.

Their C++17 example prints starting from 0. Probably a mistake.

If you look at the linked page for C++20[0], other types can be put in the initializer statement, so it's unlikely the loop auto-increments.

[0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p06...

Re: C++20 Improved the For-Loop Syntax

#9
post #3

The C++20 version is still clearly inferior to the Python and Lua examples because you still have to manually increment the counter in the loop body. IMO the sibling comment by HarHarVeryFunny has a much better C++ equivalent for this idiom, even if it's slightly more verbose.

> you still have to manually increment the counter in the loop body It doesn't look like that to me, the ++i thing seems to be just to start printing the array from 1 (I don't know how things are in Python nowadays but I know in Lua arrays start at 1, so there's no need for something like this in there), the value of i is still increasing without telling it explicitly to do so

No, the increment is necessary to keep the counter going. The choice of pre-increment versus post-increment handles the "start at 1" issue you mentioned, but it isn't auto incrementing.

Re: C++20 Improved the For-Loop Syntax

#10
post #8

Earlier quoted context omitted.

> you still have to manually increment the counter in the loop body It doesn't look like that to me, the ++i thing seems to be just to start printing the array from 1 (I don't know how things are in Python nowadays but I know in Lua arrays start at 1, so there's no need for something like this in there), the value of i is still increasing without telling it explicitly to do so

Python is 0-indexed. In OP's example, the start parameter makes i start at 1. Their C++17 example prints starting from 0. Probably a mistake. If you look at the linked page for C++20[0], other types can be put in the initializer statement, so it's unlikely the loop auto-increments. [0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p06...

So, it's just a while loop in a for loop's clothing? That's not going to be confusing for people at all...
Post reply on HN