r/cpp • u/Clean-Upstairs-8481 • Mar 22 '26
C++20 Ranges vs traditional loops: when to use std::views instead of raw loops
https://techfortalk.co.uk/2026/03/22/c20-ranges-vs-traditional-loops-when-to-use-stdviews-instead-of-raw-loops/Being an embedded software engineer I have to often work with various different types of sensor data and then process those in different ways with complex logic. I have used traditional loops for years until I came across an interesting feature of C++20. I cannot use the deal data but to demostrate here. So cooked up some fake data to make the point.
49
Upvotes
•
u/STL MSVC STL Dev Mar 23 '26
Previously posted as a text post here (removed and locked to avoid split discussion; sorry, reddit doesn't give us better tools): https://www.reddit.com/r/cpp/comments/1s0parj/c20_ranges_vs_traditional_loops_when_to_use/
13
u/MysticTheMeeM Mar 22 '26
The implication of this paragraph being that you may wish to choose between a lazily computed view and a fixed container should probably mention C++23s
std::ranges::to. Granted, this article targets C++20, but it still feels extremely relevant.5
u/Clean-Upstairs-8481 Mar 22 '26
Apologies for all the spelling errors, hopefully sorted now.
34
u/johannes1971 Mar 23 '26
I may be getting old, but I find ranges code to be an order of magnitude (i.e. not just a little bit) less readable than regular loops, and given that this is C++, I'm always worried that somewhere in there it might be reading something of which the lifetime has already expired. I see no value in saying "I'm going to filter something. Now go hunt for the filtering condition, it will be fun!" - that same thing can be expressed using a much shorter, clearer if-statement. Same goes for the transform. And each time you have one you create a new temporary name; this example has two variables named 'reading', but despite occurring in the same function, on subsequent lines, they refer to completely different entities.
The if-logic is not 'convoluted' at all. It is straightforward and clear, consisting of simple, short statements that have been in the language since the seventies. It can be parsed and understood as single statements, instead of a giant "do it all in one operation" construct that runs on and on across many lines, each more complex than the one it replaces.
Ranges code is consistently much more verbose than the equivalent using for-loops and if-statements. I have no idea why anyone would prefer it, other than "look at me being clever!".
6
u/Alarmed-Paint-791 Mar 22 '26
Consider spell checking?
6
u/sankao Mar 23 '26
Call me old fashioned, but I didn’t have to scroll horizontally to see the code with the loop version, and I had to for the ranges version, so I didn’t bother reading the text.
5
u/brookheather Mar 22 '26
You need to spell check your article - I stopped reading after "priting the values" and "vactor allocation".
2
u/Gym_Necromancer Mar 23 '26
OP, please consider joining the movement in normalizing aliasing
std::viewstorvandstd::rangestosr. It gets critical for readability in adaptor chains.3
u/MarcoGreek Mar 22 '26
Do you researched std::views::cache_latest. I would like to play with it but our compiler are too old.
0
u/Morwenn Mar 23 '26
It would be a fun experiment to see what the range version would look like with Boost.Lambda2: https://www.boost.org/doc/libs/latest/libs/lambda2/doc/html/lambda2.html