C++20 Improved the For-Loop Syntax
31–40 of 89 posts
Re: C++20 Improved the For-Loop Syntax
#32Reading 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...
Re: C++20 Improved the For-Loop Syntax
#33Just 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.
Re: C++20 Improved the For-Loop Syntax
#34Re: C++20 Improved the For-Loop Syntax
#35C++ 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); }
Re: C++20 Improved the For-Loop Syntax
#36C++ 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); }
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
#37Given both Lua and python use a enumerator, the C++ example isn't really the correct equivalent. for (auto [i, v] : std::views::enumerate(vec)) { std::cout This is how you'd do it.
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.
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
#39C++ 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…
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
#40C++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);