|
Boost : |
From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-12-06 08:35:05
----- Original Message -----
From: "the_dilster" <dylan_at_[hidden]>
> 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.
I presume you're talking about the iterator adapter library policies?
It should be pretty straightforward: You'd store the member function pointer
in the policies class. A much simpler way to get there, though, might be to
pass an instance of the standard adapters to transform_iterator or
projection_iterator, whichever is appropriate.
> 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...
???
Maybe you should describe what "pointer_iterator" is supposed to be...
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk