Ranges and/versus iterators

Fawzi Mohamed fawzi at gmx.ch
Tue Mar 23 12:45:03 PDT 2010


Andrei, as the topic just came up a comment on the range interface.
Just for plain forward iterators iterators having

bool empty()
E front()
void popFront()

makes the interface non reentrant.
For that purpose having a single function is better.
I use

	bool popFront(ref T t)
	// returns true if there is a next element, and in that case returns  
it in t

this can be used by several consumers concurrently without problems  
and creating filters, combiners,... is simple.
Another advantage is that a single object can implement several  
iterators.
A disadvantage is that even if there is a single iterator D makes type  
inference cumbersome, i.e. you cannot simply use auto, as in a loop  
you have to declare the variable before using it as the loop is
T a;
while (it.popFront(a)){
//...
}



More information about the Digitalmars-d mailing list