> 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…
C++20 Improved the For-Loop Syntax
61–70 of 89 posts
Re: C++20 Improved the For-Loop Syntax
#62C++ 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); }
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
#63Earlier quoted context omitted.
https://en.cppreference.com/cpp/io/print
Thanks. Looks like it finally made C++23!
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
#64Give 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.
Re: C++20 Improved the For-Loop Syntax
#65Just 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.
The home page of that link isn't only for C++.
Re: C++20 Improved the For-Loop Syntax
#66Only 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.
Re: C++20 Improved the For-Loop Syntax
#67Reading 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 "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
#68Even 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
Re: C++20 Improved the For-Loop Syntax
#69Even 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
#70Even 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?