Why Strings as Classes? [C++ iterator]

Michel Fortin michel.fortin at michelf.com
Thu Aug 28 04:06:59 PDT 2008


On 2008-08-28 05:47:22 -0400, Fawzi Mohamed <fmohamed at mac.com> said:

> An iterator should be like a generator, have a method next, and one 
> at_end or something similar packaged (and maybe prev() and at_start() 
> if it can also go back) in a single struct, furthermore it should work 
> seamlessly with a kind of for_each(x;iterator) construct.

I perfectly agree with this. This is why I prefer much Objective-C's 
NSEnumerator over C++ iterators: they're much simpler to use.

Some time ago, Walter asked if he could change D arrays to have 
internally an end pointer instead of a length value. He said it was to 
allow arrays, or slices, to be used as iterator efficiently. I hope he 
hasn't given up on that idea since slices are much easier to understand 
and use than C++-style iterators.


> Instead C++ choose to have begin & end iterators, simply because with 
> that construct it is trivial for the compiler to optimize it for 
> arrays, and you can use pointers as iterators without a 
> cast/constructor.

I would add that if you are using many iterators for the same array 
(vector) in a given algorithm or in function parameters, having only 
one "end" pointer save some space.


-- 
Michel Fortin  
michel.fortin at michelf.com  
http://michelf.com/




More information about the Digitalmars-d mailing list