fork download
  1. #include <iterator>
  2.  
  3. // This comes from the outer image class.
  4. typedef float color_type;
  5.  
  6. template<bool IS_CONST = false>
  7. struct Iterator : public std::iterator<std::random_access_iterator_tag, color_type>
  8. {
  9. // This is myself...
  10. typedef Iterator<IS_CONST> iterator;
  11. // ... and my variants.
  12. typedef Iterator<true> const_iterator;
  13. typedef std::reverse_iterator<iterator> reverse_iterator;
  14. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  15.  
  16. // Make base class typedefs available.
  17. typedef typename iterator::value_type value_type;
  18. typedef typename iterator::difference_type difference_type;
  19.  
  20. // My own typedefs to deal with immutability.
  21. typedef value_type const & const_reference;
  22. typedef typename std::conditional<IS_CONST, const_reference, typename iterator::reference>::type reference;
  23.  
  24. Iterator()
  25. : _position(nullptr),
  26. _step()
  27. {
  28. }
  29.  
  30. Iterator(value_type * const position, difference_type const step)
  31. : _position(position),
  32. _step(step)
  33. {
  34. }
  35.  
  36. iterator const operator-(difference_type n) const
  37. {
  38. return iterator(*this) -= n;
  39. }
  40.  
  41. difference_type operator-(iterator const rhs) const
  42. {
  43. assert(_step == rhs._step);
  44. return (_position - rhs._position) / _step;
  45. }
  46.  
  47. protected:
  48. value_type * _position;
  49. difference_type _step;
  50. };
  51.  
  52. int main()
  53. {
  54. float a = 3.141f;
  55. // Instanciate all variants.
  56. Iterator<false> empty;
  57. Iterator<true> const_empty;
  58. Iterator<false> some(&a, 5);
  59. Iterator<true> const_some(&a, 5);
  60. return 0;
  61. }
Success #stdin #stdout 0s 2892KB
stdin
Standard input is empty
stdout
Standard output is empty