Iterators and Ranges: Comparing C++ to D to Rust
Steven Schveighoffer
schveiguy at gmail.com
Mon Jun 14 14:29:57 UTC 2021
On 6/13/21 9:48 PM, Friendly Neighborhood Weirdo wrote:
> This is a talk from the 2021 c++now conference:
> https://www.youtube.com/watch?v=d3qY4dZ2r4w
The talk was really interesting and engaging. Thanks for sharing!
One thing I'll note, he said for C++ `ranges::search`, there are 6 pairs
of iterators that can be used:
first .. last
first .. mid.begin()
first .. mid.end()
mid.begin() .. mid.end()
mid.begin() .. last
mid.end() .. last
But in reality, there are 6 more:
last .. first
last .. mid.begin()
... // etc
Of course those are invalid, but the C++ compiler will happily use them,
and graciously allow memory corruption. And of course, that is the point
of D using ranges instead of iterators (maybe he glossed over it because
C++ developers take unsafety as a given).
It got me thinking again about iterators in D and how that might look
like. A lifetime ago I had a collection library (dcollections) that had
both ranges and "iterators" that I called cursors. The benefit of a
cursor is it could refer to exactly one element, and you could use them
to compose ranges from the original. In dcollections, when you did
`container.find` you got a cursor, and you can use the collection to
construct a range using 2 cursors, or remove a specific element using
the cursor. I find this functionality extremely useful, and it sucks
that D doesn't have something formal for this situation. He is right
that composing ranges from the result of algorithms is needlessly
complex in D.
I wonder if there is room for hobbled iterators (cursors) to be in D in
some capacity.
-Steve
More information about the Digitalmars-d
mailing list