title: Why can I pass iterators of two different containers into sort?
body: <p>I was taking part in an online programming contest earlier today. There was a problem where I had to deal with three input vectors and one of them required sorting. My code, in a simple example, looked as follows:</p>
  <pre><code>vector &lt;int&gt; a;
  vector &lt;int&gt; b;
  .
  .
  .
  sort(begin(a), end(b));
  </code></pre>
  <p>Notice how I mistakenly(/stupidly) made a typo <code>sort(begin(a), end(b)</code>. It took me quite a while to figure this error out because the runtime error message doesn't say anything about it.</p>
  <pre><code>c:\programming\mingw\mingw 9.2.0\include\c++\9.2.0\bits\stl_algo.h:4825:
  In function:
      void std::sort(_RAIter, _RAIter) [with _RAIter =
      __gnu_debug::_Safe_iterator&lt;__gnu_cxx::__normal_iterator&lt;int*,
      std::__cxx1998::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;,
      std::__debug::vector&lt;int&gt;, std::random_access_iterator_tag&gt;]
  
  Error: function requires a valid iterator range [__first, __last).
  
  Objects involved in the operation:
      iterator &quot;__first&quot; @ 0x000000000023FDB0 {
        type = __gnu_cxx::__normal_iterator&lt;int*, std::__cxx1998::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;
  (mutable iterator);
        state = dereferenceable (start-of-sequence);
        references sequence with type 'std::__debug::vector&lt;int, std::allocator&lt;int&gt; &gt;' @ 0x0000000000
  23FD10
      }
      iterator &quot;__last&quot; @ 0x000000000023FD80 {
        type = __gnu_cxx::__normal_iterator&lt;int*, std::__cxx1998::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;
  (mutable iterator);
        state = past-the-end;
        references sequence with type 'std::__debug::vector&lt;int, std::allocator&lt;int&gt; &gt;' @ 0x0000000000
  23FCD0
      }
  
  This application has requested the Runtime to terminate it in an unusual way.
  Please contact the application's support team for more information.
  </code></pre>
  <p>The only part I cared to look at was <code>Error: function requires a valid iterator range [__first, __last).</code> . I spent about 10 mins aimlessly thinking about what I'm doing wrong and though I glanced at the sort part a few times I failed to notice my stupid mistake. I didn't know that this was even possible (even though I've looked at the function definitions many times)!</p>
  <p>So, my question: Why do C++ algorithms allow this and why hasn't the C++ committee done something about it? Why doesn't the compiler shout? Is it deliberately implemented this way to punish people making silly mistakes using C++ like all other things in C++?</p>