C++20 Improved the For-Loop Syntax
1–10 of 89 posts
Re: C++20 Improved the For-Loop Syntax
#2for (auto [i,v] : std::views::enumerate(vec)) std::cout FWIW C++23 also has a python-like print and println:
std::println("{}: {}", i, v);
Re: C++20 Improved the For-Loop Syntax
#3Re: C++20 Improved the For-Loop Syntax
#4Re: C++20 Improved the For-Loop Syntax
#5Just what C++ needs, more bloat lol.
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
#6Re: C++20 Improved the For-Loop Syntax
#7The 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.
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
#8The 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
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
#9The 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
#10Earlier 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...