I have a legacy Buffer class and iterator for it (I use Visual Studio 2013 - Windows XP v120_xp toolset, so I suppose I use c++11 standard):
template <typename T>
class PtrIterator : public std::iterator<std::forward_iterator_tag, T> {
typedef PtrIterator<T> iterator;
pointer pos_;
public:
PtrIterator() : pos_(nullptr) {}
explicit PtrIterator(T* v) : pos_(v){}
~PtrIterator() = default;
iterator operator++(int) // postfix
{ return pos_++; }
iterator& operator++() // prefix
{
++pos_;
return *this;
}
reference operator*() const
{ return *pos_; }
pointer operator->() const
{ return pos_; }
iterator operator+(difference_type v) const
{ return pos_ + v; }
bool operator==(const iterator& rhs) const
{ return pos_ == rhs.pos_; }
bool operator!=(const iterator& rhs) const
{ return pos_ != rhs.pos_; }
};
using BufferIterator = PtrIterator<uint8_t>;
class Buffer{
public:
// ...
BufferIterator begin() const;
BufferIterator end() const;
}
Now I'm wonder, if it possible to get rid of custom iterator and use standard one. I'm trying to do the following thing:
using BufferIterator = std::iterator<std::random_access_iterator_tag, uint8_t, ptrdiff_t, uint8_t*, uint8_t&>;
Compiler says there are no operators for BufferIterator
, such as operator==
and so on. But I don't know how such declarations looks like (note, that my classes are in a namespace). I tried the following one:
bool operator!=(const og::BufferIterator& lhs, const og::BufferIterator& rhs)
{ return false; }
, but that doesn't work.
So, how to fit standard iterator for my needs?