Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

71–80 of 89 posts

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

#71
post #42

Earlier quoted context omitted.

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

It's "it" as in the pronoun, not as in "iterator".

We call that “Larry Wall nomenclature”.

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

#72
post #36

Earlier quoted context omitted.

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.

Go is similar: `for/range` has always worked on arrays, slices, maps, strings and channels, and they extended it to integers in version 1.22 and to user-defined generators in version 1.23. It generates one or two outputs depending on the type of the RHS, but one can assign the index value to _ to ignore it. Among standard types, only channels and integers generate one output; containers (including strings) give an index or key and a value.

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

#73
post #62

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

C++23 example, import std; using namespace std; int main() { vector vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (auto&& [i, it] : vec | std::views::enumerate) println ("{}:{}", ++i, it); } As I keep mentioning, the features being copied, slowly erode D's relevance.

[deleted]

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

#74
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…

I find Go’s version worse in that it does not allow you to elide the index (which is not needed most of the time). You have to write `for _, s := range vec`, as `for s := range vec` makes s the index.

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

#75

Earlier quoted context omitted.

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.

I think C++ needs more of `:`, `::` and maybe new operators `&&&` and `&&&&`.

C++32 will look like

  for [auto&&&&]:::static (std::for_enumeration_initializer &&⁢ @auto: {{const signed&&&&& i const}}) { }

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

#76
the looping facilities in most languages suck. We are at a position where iteration is either "increment or decrement numbers", or "go through this collection". Everything else means degrading performance. In some languages (Python, ruby) the choice is between the comfortable way to iterate through a collection (for a in xyz:), or the fast way (while). It is just stupid.

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

#77
post #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”.

Haha I always interpreted "it" to mean literally the word "it". I didn't realize it's a short-hand form for iterator, but now I'm not so sure if that's universal or if there's a genuine split.

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

#78
post #61

Earlier quoted context omitted.

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-bake…

The same organisations are behind Rust Foundation, and are responsible for the LLVM and GCC compilers, written in C++, rustc depends on.

[dead]

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

#79
post #59

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.

Some folks don't like iostreams, I keep using them, since Turbo C++ 1.0 for MS-DOS. Although I would probably use std::print in more modern compilers. D needs to work into its marketing, all key features that made it relevant back in 2011 with Andrei's book have now been copied even if badly, across all mainstream languages.

> Some folks don't like iostreams

I thought it was ugly in 1987, and it hasn't improved with age. Then for templates made it worse.

Then there's formatted I/O:

    void IOS_precision()
    {
    cout 
and I don't know if the problem with multithreading was resolved or not.

> copied even if badly

Any program can be written in any language. But why suffer?

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

#80
post #62

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

C++23 example, import std; using namespace std; int main() { vector vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (auto&& [i, it] : vec | std::views::enumerate) println ("{}:{}", ++i, it); } As I keep mentioning, the features being copied, slowly erode D's relevance.

If you prefer writing:

    for (auto&& [i, it] : vec | std::views::enumerate)
        println ("{}:{}", ++i, it);
instead of:

    foreach (i, s; vec)
        writeln(i, ": ", s);
well, what can I say?
Post reply on HN