Find Related products on Amazon

Shop on Amazon

Using Token Sequences to Iterate Ranges

Published on: 2025-05-04 18:17:25

Using Token Sequences to Iterate Ranges There was a StackOverflow question recently that led me to want to write a new post about Ranges. Specifically, I wanted to write about some situations in which Ranges do more work than it seems like they should have to. And then what we can do to avoid doing that extra work. I’ll offer solutions — one sane one, which you can already use today, and one pretty crazy one, which is using a language feature we’re still working on designing, which may not even exist in C++29. The Problem For the purposes of this post, I’m just going to talk about the very simple problem of: for (auto elem : r) { use(elem); } In the C++ iterator model, this desugars into something like: auto __it = r.begin(); auto __end = r.end(); while (__it != __end) { use(*__it); ++__it; } I used a while loop here deliberately, because it’s a simpler construct and it lets me write the advance step last. Now, if you want to customize the behavior of a range, those are your en ... Read full article.