C++20 Improved the For-Loop Syntax
41–50 of 89 posts
Re: C++20 Improved the For-Loop Syntax
#42Reading this, I really can't make much sense of it: for (int i=0; auto&& it: vec) cout 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
Re: C++20 Improved the For-Loop Syntax
#43C++20 also has an enumerate() generator, so if you like the python syntax you can just do: for (auto [i,v] : std::views::enumerate(vec)) std::cout FWIW C++23 also has a python-like print and println: std::println("{}: {}", i, v);
> std::cout C++ needs to abandon iostreams. Didn't the C++ community acknowledge that it was a bad idea? In the early days of D, people did want to do a version of it for D, but I objected and currently nobody wants it.
Re: C++20 Improved the For-Loop Syntax
#44Re: C++20 Improved the For-Loop Syntax
#45Only people who saw nothing bad in passing pairs .begin(), .end() in tons of places in c++ for like 30 years can say that thing like ‘auto&&’ improves anything.
`auto&&` has been standard syntax in C++ for going on 15 years now and has a very clear, irreplaceable meaning - a type-deduced forward reference - to any remotely competent developer.
Re: C++20 Improved the For-Loop Syntax
#46Earlier quoted context omitted.
So, it's just a while loop in a for loop's clothing? That's not going to be confusing for people at all...
No, it's a for loop that happens to include an unrelated variable declaration.
Re: C++20 Improved the For-Loop Syntax
#47Earlier quoted context omitted.
> std::cout C++ needs to abandon iostreams. Didn't the C++ community acknowledge that it was a bad idea? In the early days of D, people did want to do a version of it for D, but I objected and currently nobody wants it.
https://en.cppreference.com/cpp/io/print
Re: C++20 Improved the For-Loop Syntax
#48Earlier quoted context omitted.
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...
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.
Which 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.
Re: C++20 Improved the For-Loop Syntax
#49C++ should copy D's elegance: import std.stdio; string[9] vec = [ "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" ]; void main() { foreach (i, s; vec) writeln(i, ": ", s); }
Re: C++20 Improved the For-Loop Syntax
#50This is a Fine addition to c++ due to how c++ uses lifetimes for resource management but the syntax is convoluted and the claim that this is intended to enable enumeration is... Just silly.