I am implementing a custom input iterator. It does not store the current value in a member variable to save memory. Thus, correctly implementing operator->
becomes non-trivial. Here is what I currently have (inspired by this Boost code linked from this question):
template<class ForwardIterator>
struct custom_input_iterator {
typedef const std::basic_string<typename std::iterator_traits<ForwardIterator>::value_type> value_type;
// All the other stuff required for an input iterator
struct arrow_operator_proxy {
explicit arrow_operator_proxy(value_type value)
: value(value)
{}
const value_type * operator->() const {
return &value;
}
private:
value_type value;
};
arrow_operator_proxy operator->() const {
// we can assume that this is a valid range here
return arrow_operator_proxy(value_type(begin, current_sequence_end));
}
private:
ForwardIterator begin;
ForwardIterator current_sequence_end;
};
I have two questions about this:
- Is this correct with respect to input iterator requirements and object lifetimes?
- Is there a simpler implementation?