我正在实现一个自定义输入迭代器。它不会将当前值存储在成员变量中以节省内存。因此,正确实施operator->
变得非常重要。以下是我目前所拥有的内容(受this Boost code链接的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;
};
我有两个问题: