Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

31–40 of 89 posts

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

#31
I guess I am officially old and no longer know what the cool "it" is any more. The C++17 example seems much more clear to me. It is more typing, but so what? It is not that much more typing, and it looks like code that people have written for 40 years.

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

#32
post #28

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

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.

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

#33
post #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.

And it's not because he's cranky or something, he was holding out for something worthy to possibly use instead of C. Turned out to be Rust.

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

#36

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

Go is similar:

  package main

  import "fmt"

  var vec = []string{"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}

  func main() {
    for i, s := range vec {
      fmt.Printf("%d: %s\n", i, s)
    }
  }
Using `fmt.Println(i, ":", s)` inserts spaces on both sides of the colon, so the output is not quite right.

I find Go's `range` syntax easier to read than D's `(i, s; vec)` -- although generator functions that work with `range` have unaesthetic return types.

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

#38

> It seems to me that the C++ Standards Committee is doing a decent job maintaining the language, and introducing useful features when it makes sense to do so. This can’t be further from truth. C++ is essentially Frankenstein’s monster.

I believe one of the main goals of whatever shadowy organizations are behind the "standards committee" is to sabotage the language to help clear the way for Rust.

If these people really have a great idea for what the future C++ should be like, then they should introduce that language all at once. Let us see their grand vision in one big package that can be embraced or rejected. This steady drip-drip-drip of half-baked features every few years is creating one hellish language.

I spend a lot of time in the Chromium source code and have seen plenty of boneheaded code, especially in places like Autofill where they seem to turn the newbies loose, but also in surprising places like V8, due to newbs using newfangled C++ "features" to do what worked perfectly well before in older idioms and was easier to read also.

Take for instance the function TryReduceFromMSB in v8/src/compiler/turboshaft/wasm-shuffle-reducer.cc -- do you see any reason that std::optional max; couldn't just be a regular uint8_t? Let's name it 'max_used' also so the name is more descriptive and doesn't clobber the namespace.

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

#39
post #36

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

Go is similar: package main import "fmt" var vec = []string{"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"} func main() { for i, s := range vec { fmt.Printf("%d: %s\n", i, s) } } Using `fmt.Println(i, ":", s)` inserts spaces on both sides of the colon, so the output is not quite right. I find Go's `range` syntax easier to read than D's `(i, s; vec)` -- although generator functions that work wi…

D has several usages for `foreach` depending on the number and type of its arguments. In this case, it sees that `vec` is an array, and so constructs a loop over the array. If `vec` was a range, the loop will be constructed as a loop over the array.

It's nice because it makes the syntax for arrays and ranges interchangeable, making for easy refactoring.

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

#40

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.
Post reply on HN