Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

61–70 of 89 posts

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

#61

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

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

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

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

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

#63

Earlier quoted context omitted.

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

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

fmt:print from fmtlib has been around for at least a decade, though having it be in the spec by default now is nice.

Even in c++20, you could use format() to do most of what you'd want from it.

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

#64
post #30

Give me the explicit C syntax any day over this monstrosity. I refuse to write anything other than C++98, the last version of C++ that built on C without trying to turn it into a completely different language.

So K&R C it is, nothing of that C23 crazy stuff.

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

#65
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.

Java, Python, C#, Common Lisp, Perl, also deserve guides.

The home page of that link isn't only for C++.

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

#66
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.

and completely unnecessary in that example, it's a string, type string, makes it way easier to understand

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

#67

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

As someone who wrote a lot of C and legacy C++, I find the first form easier to understand.

The "int i=0; ... ++i" part is just your typical C-style loop. And the "auto&& it: vec" is the for-each kind of loop that dates back from C++11. The only new thing is that you can do both at the same time, which is not much of a stretch.

On the second one, I had to look up what the "std::views::enumerate" did. It is kind of obvious when you look at the code, but it is not as explicit.

And maybe most importantly, it doesn't do the same thing!

The first loop starts at one, you can tell because it is ++i, not i++. A debatable choice when it comes to readability, but you can see it right before your eyes. For the second loop, I had to, again, look up to make sure the result of "std::views::enumerate" was indeed zero-indexed (and it is, of course).

Which form you prefer depends on if you like being explicit or if you prefer abstractions. I usually prefer the former. Performance-wise, the generated code is probably very close, but the first one is likely to have an advantage on debug (unoptimized) builds.

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

#68
post #57

Even more modern, we are getting C++26 nowadays. CTADs for vector (no need for explicit string), ranges enumeration, and range-based for-loop with structured types int main() { vector vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (auto&& [i, it] : vec | std::views::enumerate) println ("{}:{}", ++i, it); } Compiler explorer example, https://cpp.godbolt.org/z/3r8rPdjbG

I'm super confused. In your example, ++i is equivalent to (i+1) and the loop updates i itself. But with the syntax in the blogpost, i is only initialized, and it is the ++i that updates the value?

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

#69
post #68
post #57

Even more modern, we are getting C++26 nowadays. CTADs for vector (no need for explicit string), ranges enumeration, and range-based for-loop with structured types int main() { vector vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (auto&& [i, it] : vec | std::views::enumerate) println ("{}:{}", ++i, it); } Compiler explorer example, https://cpp.godbolt.org/z/3r8rPdjbG

I'm super confused. In your example, ++i is equivalent to (i+1) and the loop updates i itself. But with the syntax in the blogpost, i is only initialized, and it is the ++i that updates the value?

Yes. You don’t sound that confused?

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

#70
post #68
post #57

Even more modern, we are getting C++26 nowadays. CTADs for vector (no need for explicit string), ranges enumeration, and range-based for-loop with structured types int main() { vector vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (auto&& [i, it] : vec | std::views::enumerate) println ("{}:{}", ++i, it); } Compiler explorer example, https://cpp.godbolt.org/z/3r8rPdjbG

I'm super confused. In your example, ++i is equivalent to (i+1) and the loop updates i itself. But with the syntax in the blogpost, i is only initialized, and it is the ++i that updates the value?

Copy paste error, too late to update the comment, it should be a plain i.
Post reply on HN