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

Steven Schveighoffer schveiguy at gmail.com
Mon Jun 14 15:12:56 UTC 2021


On 6/14/21 10:46 AM, Ola Fosheim Grøstad wrote:
> 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.

I'm referring to the talk, where you can use one function (search) to 
compose any possible "range" from the result that makes sense, but also 
ones that don't make sense. And D doesn't allow that specifically due to 
the safety implications.

A C++ "safe" iterator that is essentially a range under the hood doesn't 
compose any better than D ranges.

-Steve


More information about the Digitalmars-d mailing list