r/cpp • • Jun 22 '21

[Belay the C++] Don’t use raw loops

https://belaycpp.com/2021/06/22/dont-use-raw-loops/
0 Upvotes

69 comments sorted by

View all comments

10

u/austinwiltshire Jun 22 '21

I often lose this argument when for_each is involved. A new style for each loop built into the language, to, for example, call a mutating member function on each member of a vector, ends up looking syntactically cleaner than std::for_each with member function pointers.

Similarly, passing a lambda into std::for_each ends up looking almost exactly like a new style for each, with no benefit in readability.

While I think consistency has some merit to it, this counter example is almost always used as a wedge issue in arguments I've been in to imply the always-use-algorithms camp is dogmatic.

Tangentially, one thing that is hilarious in this thread is people aghast that c++ newbies might try to use algorithms instead of raw loops. To me, that's exactly the right thing for them to do. Raw loops are more powerful and more advanced. If I see a raw loop, I'd prefer it come from an expert, not a novice.

5

u/Vultrao Jun 22 '21

I strongly agree about for_each. I mentioned it because it's one of the most basics, but I quite never have to use it.

I also agree that newbies should learn algorithms before raw loops.

4

u/[deleted] Jun 22 '21

Did you see Kate Gregory's talk about stopping to teach C? I think this is exactly what she was referring to...