Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

41–50 of 89 posts

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

#42

Reading 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

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”.

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

#43

C++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.

https://en.cppreference.com/cpp/io/print

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

#45
post #14

Only 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.

What exactly is your complaint with it?

`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

#46
post #13
post #10

Earlier 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.

Ah, I see. The increment is so they can manually handle the included feature of the other cases, which number of the iteration you're on. Nice that it will automatically loop across a collection, annoying that they couldn't go the whole way and you need to deal with it manually.

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

#47

Earlier 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

Thanks. Looks like it finally made C++23!

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

#48
post #28

Earlier 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.

> That one actually shows what the variable is initialized to. This one doesn't. reply

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

#49

C++ 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); }

I don't like that semi-colon. AFAICT `i, s;` is not an independent statement. `i, s: vec` would have been fine, but `;` ?!?! madness.

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

#50
You can do this right now with an iterator adaptor and some range trickery. The c++ range based for loop already enables the same behavior as python. The only thing missing is a standard enumerate function.

This 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.

Post reply on HN