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