title: Implementing custom iterator to work with std::sort
body: <p>My aim is to learn how to write a custom iterator from scratch. I have written the following iterator:</p>
  
  <pre><code>#include &lt;iterator&gt;
  
  template&lt;class D&gt;
  class SpanIterator final : public std::iterator&lt;std::random_access_iterator_tag, D&gt;
  {
  private:
      D* _data;
  
  public:
      explicit SpanIterator(D* data) :
          _data{ data }
      {
      }
  
      SpanIterator(const SpanIterator&amp; itertator) = default;
  
      SpanIterator&amp; operator=(const SpanIterator&amp; iterator) = default;
  
      SpanIterator&amp; operator=(D* data)
      {
          _data = data;
  
          return *this;
      }
  
      operator bool() const
      {
          return _data != nullptr;
      }
  
      bool operator==(const SpanIterator&amp; itertator) const
      {
          return _data == itertator._data;
      }
  
      bool operator!=(const SpanIterator&amp; itertator) const
      {
          return _data != itertator._data;
      }
  
      SpanIterator&amp; operator+=(const std::ptrdiff_t&amp; movement)
      {
          _data += movement;
  
          return *this;
      }
  
      SpanIterator&amp; operator-=(const std::ptrdiff_t&amp; movement)
      {
          _data -= movement;
  
          return *this;
      }
  
      SpanIterator&amp; operator++()
      {
          ++_data;
  
          return *this;
      }
  
      SpanIterator&amp; operator--()
      {
          --_data;
  
          return *this;
      }
  
      SpanIterator operator++(int)
      {
          auto temp = *this;
  
          ++_data;
  
          return temp;
      }
  
      SpanIterator operator--(int)
      {
          auto temp = *this;
  
          --_data;
  
          return temp;
      }
  
      SpanIterator operator+(const std::ptrdiff_t&amp; movement)
      {
          auto oldPtr = _data;
  
          _data += movement;
  
          auto temp = *this;
  
          _data = oldPtr;
  
          return temp;
      }
  
      SpanIterator operator-(const std::ptrdiff_t&amp; movement)
      {
          auto oldPtr = _data;
  
          _data -= movement;
  
          auto temp = *this;
  
          _data = oldPtr;
  
          return temp;
      }
  
      D&amp; operator*()
      {
          return *_data;
      }
  
      const D&amp; operator*() const
      {
          return *_data;
      }
  
      D&amp; operator-&gt;()
      {
          return _data;
      }
  };
  </code></pre>
  
  <p>Which I am testing like so:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  
  int main()
  {
      std::array&lt;double, 3&gt; values = { 1, 2, 1 };
  
      SpanIterator&lt;double&gt; begin{ values.data() };
      SpanIterator&lt;double&gt; end{ values.data() + values.size() };
  
      std::sort(begin, end);
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>However it fails to compile, giving the following errors:</p>
  
  <ul>
  <li>Error    C2666   'SpanIterator::operator -': 2 overloads   </li>
  <li>Error    C2780   'void std::_Sort_unchecked1(_RanIt,_RanIt,_Diff,_Pr &amp;)':
  expects 4 arguments - 3 provided</li>
  </ul>
  
  <p>If I remove <code>SpanIterator operator-(const std::ptrdiff_t&amp; movement)</code> I get different errors:</p>
  
  <ul>
  <li>'void std::_Guess_median_unchecked(_RanIt,_RanIt,_RanIt,_Pr &amp;)':
  could not deduce template argument for '_RanIt' from 'int'</li>
  <li>'_Guess_median_unchecked': no matching overloaded function found</li>
  <li>Error    C2100   illegal indirection</li>
  </ul>