#include <iterator>
#include <iostream>
// This comes from the outer image class.
typedef float color_type;
template<bool IS_CONST = false>
struct Iterator : public std::iterator<std::random_access_iterator_tag, color_type>
{
// This is myself...
typedef Iterator<IS_CONST> iterator;
// ... and my variants.
typedef Iterator<true> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
// My own typedefs to deal with immutability.
typedef value_type const & const_reference;
typedef typename std::conditional<IS_CONST, const_reference, typename iterator::reference>::type reference;
Iterator()
: _position(nullptr),
_step()
{
}
Iterator(value_type * const position, difference_type const step)
: _position(position),
_step(step)
{
}
iterator const operator-(difference_type n) const
{
return iterator(*this) -= n;
}
difference_type operator-(iterator const rhs) const
{
assert(_step == rhs._step);
return (_position - rhs._position) / _step;
}
protected:
value_type * _position;
difference_type _step;
};
int main()
{
float a = 3.141f;
// Instanciate all variants.
Iterator<false> empty;
Iterator<true> const_empty;
Iterator<false> some(&a, 5);
Iterator<true> const_some(&a, 5);
Iterator<>::difference_type diff_type = Iterator<>::difference_type();
Iterator<>::value_type val_type = Iterator<>::value_type();
std::cout << diff_type << " " << val_type << std::endl;
return 0;
}