Saturday 15 June 2013

c++ - Implementing member access operator-> for ForwardIterator with on-the-fly computed temporary values -



c++ - Implementing member access operator-> for ForwardIterator with on-the-fly computed temporary values -

i'm implementing iterator adaptor range lazily evaluates on original range. means: dereferencing iterator should dereference underlying iterator , apply operation on result, returning result of operation.

t operator*() const { homecoming someoperation(*original_iterator); }

how can implement operator-> analogous operator*? when looking @ other iterator's implementations, homecoming t*. can't homecoming pointer, since "pointed object" temporary, computed on-the-fly.

what's usual guidance in case? may homecoming t instead?

although don't need operator (i utilize (*i).m instead of i->m , standard algorithms don't seem depend on -> either), i'd iterator strictly conform forwarditerator concept specialization of inputiterator requires implementation of operator.

the simplest way utilize boost.iterators implement iterators, takes care of tricky code.

but technique used boost.iterators isn't complicated. looks this:

template <typename t> class proxy_holder { t t; public: proxy_holder(const t& t) : t(t) {} t* operator ->() const { homecoming &t; } }; class the_iterator { // ... proxy_holder<my_proxy> operator ->() const { homecoming proxy_holder<my_proxy>(**this); } };

this relies on fact arrow operators chained, i.e. if 1 arrow operator returns isn't raw pointer, arrow operator called on thing in turn, until pointer.

c++ iterator operator-overloading

No comments:

Post a Comment