Iterators and Ranges: Comparing C++ to D to Rust

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Jun 14 14:46:01 UTC 2021


On Monday, 14 June 2021 at 14:29:57 UTC, Steven Schveighoffer 
wrote:
> 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).

Apples and oranges.

Gang of Four defined _iterator_ to be what D and C++ call 
"ranges", basically a "heavy" object that has builtin knowledge 
of how to traverse a data-structure. Of course, the term "range" 
is a misnomer since you can traverse a graph in numerous ways, so 
you can have a depth-first-iterator, breadth-first-iterator, and 
indeed have iterators that to randomized walks... so iterator is 
a better name.

C++ STL "iterators" are *light-weight* pointer-abstractions. You 
can create heavy C++ "iterators" that keeps track of where "the 
end" is and have it throw an exception. C++ does support safe 
iterators. STL does not.




More information about the Digitalmars-d mailing list