[comp.lang.c++] Iterators

gjditchfield@watmsg.waterloo.edu (Glen Ditchfield) (07/06/89)

In article <6590181@hplsla.HP.COM>, jima@hplsla (Jim Adcock) writes:
>The emerging standard for iterators seems to be overloading operator()()
>to provide interation.
   I'd rather standardize on operator>>().  I think an iterator is easily
viewed as a kind of stream.  Operator>>() also makes it easier to have
iterators that return things other than ints or pointers.

	// This isn't quite right, but you get the idea...
	class Element {...};
	class Bag {
		friend class BagGenerator;
		...
	class BagGenerator {
	    public:
		BagGenerator(Bag&);
		BagGenerator& operator>>(Element&);
		int operator int();
		};
	...
	Bag     b;
	Element e;
	...
	for (BagGenerator bg(b); bg >> e;) { ... };

I had some fun using this approach to make up parameterized container
classes (using <generic.h>) with associated generator classes.

In article <19176@paris.ics.uci.edu> schmidt@zola.ics.uci.edu (Doug Schmidt) writes:
>There is a group of researchers at the University of Wisconsin that
>are implementing an interesting C++ superset called E (I guess that's
>really (C += 2) ;-)).  One useful extension they've added is Clu-style
>iterators.
   I think Alphard-style generators are a better approach for object-
oriented languages.  In Alphard terms, a generator is an object that gives
access to successive elements of a collection, and an iterator is a control
structure that uses a generator.  The important point is that a generator
type is a subtype of the collection's element type.  When you apply the
element type operations to a generator, they use the value of the current
collection element.  Generators also provide "init", "next" and "done?"
operations that are implicitly used by iterators.
   This won't work so well in C++, because subtypes of builtin types aren't
allowed.  Alphard generators also make use of parameterized types definitions.

    Glen Ditchfield  gjditchfield@violet.uwaterloo.ca  Office: DC 2517
Dept. of Computer Science, U of Waterloo, Waterloo, Ontario, Canada, N2L 3G1
	"... and the rest, we will spend foolishly!" -- _Svengali_