Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-10-02 16:54:36


Yes, I'd like to see a solution to this problem too...

As for backwards compatibility: I've got a bunch of code that uses
iterator_adaptor... I don't mind changing it as long as the change is
straightforward and results in nicer code overall.

On Tue, 2 Oct 2001, David Abrahams wrote:

david.> ----- Original Message -----
david.> From: <michel.andre_at_[hidden]>
david.>
david.> > > template <class Difference, class BaseType1, class BaseType2>
david.> > > Difference distance(type<Difference>, const BaseType1& x, const
david.> > > BaseType2& y) const {}
david.> > >
david.> > > As I understand it, the "BaseType1/2" is misleading, because the
david.> > > parameter is not the base iterator, it's the adapted iterator. Do I
david.> > > understand correctly? I hope so, because it would be impossible to
david.> > > make this work for adaptors that have internal state otherwise.
david.> >
david.> > It seems from the source code that it is the Base or underlying
david.> > iterator thats sent into to the policy class.
david.> >
david.> > I'm wrestling with a similar problem and the iterator_adaptors i want
david.> > to keep all state in the "begin()" iterator and have the "end()" with
david.> > just a flag saying endIterator and this means to be able to determine
david.> > equality i need the policy objects for both rhs and lhs in the equal
david.> > method of the policy class.
david.> >
david.> > template <class Iterator1, class Iterator2>
david.> > bool equal(const Iterator1& x, const Iterator2& y) const
david.>
david.> This is a problem which occurred to me long after Jeremy started storing a
david.> policies object in the iterator adaptor so he could keep extra state. Hey,
david.> Jeremy!
david.>
david.> > The reason for my design is that the iterator generated by begin
david.> > contains both the start and the end of the iteration and in some
david.> > cases even a pointer to the container of the elements to iterate over.
david.> >
david.> > Is there some solution to this problem? Or do i have to "adapt" the
david.> > base iterator with my state (using eg inheritance) and then use the
david.> > iterator_adapter to adapt my "adaption" to get it to work?
david.>
david.> If inheritance works for you (remember, you can't inherit from a pointer),
david.> then go for it. That seems like the easiest approach. Otherwise, consider
david.> std::pair<IteratorType,StateData> or the equivalent.
david.>
david.> We should probably think about changing the design. I think a
david.> backwards-compatible change is possible, but I don't think the extra cruft
david.> implied is desirable:
david.>
david.> struct default_iterator_policies
david.> {
david.> // Some of these members were defined static, but Borland got confused
david.> // and thought they were non-const. Also, Sun C++ does not like static
david.> // function templates.
david.>
david.> template <class Base>
david.> void initialize(Base&)
david.> { }
david.>
david.> // The "type<Reference>" parameter is a portable mechanism for
david.> // the iterator_adaptor class to tell this member function what
david.> // the Reference type is, which is needed for the return type.
david.> template <class Reference, class Base>
david.> Reference dereference(type<Reference>, const Base& x) const
david.> { return *x; }
david.>
david.> template <class Iterator>
david.> void increment2(Iterator& x)
david.> { x.policies().increment(x.iter()); }
david.>
david.> template <class Base>
david.> void increment(Base& x)
david.> { ++x; }
david.>
david.> template <class Iterator>
david.> void decrement2(Iterator& x)
david.> { x.policies().decrement(x.iter()); }
david.>
david.> template <class Base>
david.> void decrement(Base& x)
david.> { --x; }
david.>
david.> template <class Iterator, class DifferenceType>
david.> void advance2(Iterator& x, DifferenceType n)
david.> { x.policies().advance(x.iter(), n); }
david.>
david.> template <class Base, class DifferenceType>
david.> void advance(Base& x, DifferenceType n)
david.> { x += n; }
david.> .
david.> .
david.> .
david.>
david.> You get the idea. Now you can redefine difference2 instead of difference to
david.> get the behavior you want. Hmm, have I just invented a new kind of
david.> compile-time virtual function here? ;-)
david.>
david.> Anyway, I'd like to discuss with the group whether backwards-compatibility
david.> is paramount. Should we maintain the status quo, crufty-up the design and
david.> the documentation to maintain backward-compatibility, provide a transition
david.> path using #ifdefs, or something else?
david.>
david.> ===================================================
david.> David Abrahams, C++ library designer for hire
david.> resume: http://users.rcn.com/abrahams/resume.html
david.>
david.> C++ Booster (http://www.boost.org)
david.> email: david.abrahams_at_[hidden]
david.> ===================================================
david.>
david.>
david.>
david.> Info: http://www.boost.org Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
david.>
david.> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
david.>
david.>

----------------------------------------------------------------------
 Jeremy Siek http://php.indiana.edu/~jsiek/
 Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
 C++ Booster (http://www.boost.org) office phone: (812) 855-9761
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk