News for hackers. Stuff that matters.
Re:Modern C++ gamedev: thoughts and misconceptions by badsectoracula on Saturday May 16, 2020 @11:48AM (#23204377)
Yeah, this is exactly what i dislike - unless the declaration of "vec" is somewhere close by (and assuming it isn't itself "auto" :-P) you have no idea what "i" is.

Especially when that "auto i : vec" should have instead been "auto& i : vec" or "const auto& i : vec" and now you are at best wasting cycles and at worst writing to copies that will soon be discarded, ending up with a bug that can be very hard to spot.

Re:Modern C++ gamedev: thoughts and misconceptions by klyrs on Saturday May 16, 2020 @12:45PM (#23205001)
I love the idea of auto range-based iteration but it's full of warts like the one you mention. Recently I found myself wanting an iterator over a combinatorial family.

Generation of a single solution: 3 easy lines (calling on a few hundred lines of goofy math that actually describes the structure, but that's common to all of these approaches)

Writing a for-loop to fill a std::vector of solutions -- about 10 lines of a familiar stack-walking pattern which could confuse a novice.

Making a fake container that defines a begin() and end() along with a nested iterator class: about 20 lines of necessary boilerplate, another 20 lines to replicate the stack-walking, now sprinkled about the boilerplate. The novice is completely bewildered, so we add another 10-20 lines of comments to explain it.

So I have this strong urge to keep the first two implementations in place, just to provide a gentler ramp. But I won't use the code in the end, so it would only add maintenance overhead, so a lone tear rolls down my cheek as I delete the clear, readable code.

In python, this is often as easy as changing square brackets to parentheses to change a list comprehension into a generator.

Re:Modern C++ gamedev: thoughts and misconceptions by asveikau on Saturday May 16, 2020 @12:44PM (#23204994)
I usually write auto &i : vec out of reflex, but left the reference part out into visually match what the parent had, with no reference. (But an iterator, so not having that issue.)
Re:Modern C++ gamedev: thoughts and misconceptions by eska on Saturday May 16, 2020 @12:26PM (#23204802)
If the type is not obvious one can also write

    for(Class entry : container)
This is still an uncontroversial improvement over having to typedef or use auto for the iterator.
Re:Modern C++ gamedev: thoughts and misconceptions by badsectoracula on Sunday May 17, 2020 @04:27PM (#23215136)
Sure, that is what i'd probably write myself too.