Implementing Member Access Operator & # 8594; for ForwardIterator with computed time values ​​"on the fly"

I am implementing an iterator for a range of adapters that lazily evaluates something in the original range. This means: dereferencing an iterator should dereference the base iterator and apply some operation to the result, and then return the result of this operation.

T operator*() const {
    return someOperation(*original_iterator);
}

How can I implement operator->similar to this operator*? If you look at other iterator implementations, they usually return T*. But I cannot return the pointer, since the "pointer to the object" is temporary, calculated on the fly.

What is the usual guide in this case? Can I just get it back T?

Although I personally do not need this operator (I could also use (*i).minstead i->m, and the standard algorithms do not seem to depend on either ->), I would like my iterator to strictly correspond to the concept ForwardIterator, which is a specialization InputIteratorthat requires the implementation of this operator.

+4
source share
1 answer

The easiest way is to simply use Boost.Iterators to implement your iterators, which takes care of complex code.

But the technique used by Boost.Iterators is not so complicated. It looks like this:

template <typename T>
class proxy_holder {
  T t;
public:
  proxy_holder(const T& t) : t(t) {}
  T* operator ->() const { return &t; }
};

class the_iterator {
  // ...
  proxy_holder<my_proxy> operator ->() const {
    return proxy_holder<my_proxy>(**this);
  }
};

, , .. , , , .

+5

All Articles