我正在为自定义容器定义迭代器。迭代器实现了 InputIterator 和 OutputIterator 概念。
iterator::reference
和应该使用什么类型const_iterator::reference
?operator*
foriterator
和应该返回什么const iterator
类型const_iterator
?
在这种iterator
情况下,是否应该有两个定义operator*
?如下(T
是包含的值类型):
template<typename T>
class iterator {
public:
using reference = T&;
...
T& operator*() { return ...reference to the underlying storage... }
const T& operator*() const { return ...const reference to the underlying storage... }
...
};
或者只有一个:
T& operator*() const { return ...reference to the underlying storage... }
换句话说,是否const iterator
允许对底层容器进行可变访问?
对于这种const_iterator
情况,我是否正确猜测以下内容是正确的?
template<typename T>
class const_iterator {
public:
using reference = const T&;
...
const T& operator*() const { return ...reference to the underlying storage... }
...
};
还是应该const_iterator::reference
没有T&
的定义const
?
除了完整的答案之外,我还希望能参考权威来源,以便将来可以查找类似信息。