Boost logo

Boost :

From: the_dilster (dylan_at_[hidden])
Date: 2001-12-05 23:39:09


I know the existing adapters can be used to access members and call
member functions, but they are (to me) exceedingly clumsy to use.

I managed to write templates that allow you to do both:

member_iterator<string, list_iterator>
        person_first(people.begin(), &person::m_name),
        person_last(people.end());

copy(person_first, person_last, ostream_iterator<string>(cout, "\n"));

AND

memfun_iterator<const string&, list_iterator>
        person_first(people.begin(), &person::name),
        person_last(people.end());

copy(person_first, person_last, ostream_iterator<string>(cout, "\n"));

(you need a const_memfun_iterator too).
But I can't see any straightforward way of implementing these using
the policy templates.

For the curious, my initial implementation is exceedingly simple, ie:

template <class T, class I, class C = I::value_type>
struct member_iterator : I
{
  member_iterator() { }
  member_iterator(const I& i, T C::* m = 0) : I(i), m_member(m) { }
  T& operator* () { return I::operator*().*m_member; }
private:
  T C::* m_member;
};

Which requires the use of a pointer_iterator wrapper for vectors (I
can't see how to avoid this anyway for certain compilers that don't
support partial specialisation). It would be nice
if "pointer_iterator" was standard actually...or at least in boost...

Dylan


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