更简单的运营商实施 - >对于输入迭代器

时间:2014-11-17 22:35:11

标签: c++ iterator

我正在实现一个自定义输入迭代器。它不会将当前值存储在成员变量中以节省内存。因此,正确实施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;
};

我有两个问题:

  1. 输入迭代器要求和对象生命周期是否正确?
  2. 是否有更简单的实施?

0 个答案:

没有答案