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:
vector <int> a;
vector <int> b;
.
.
.
sort(begin(a), end(b));
Notice how I mistakenly(/stupidly) made a typo sort(begin(a), end(b). It took me quite a while to figure this error out because the runtime error message doesn't say anything about it.
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<__gnu_cxx::__normal_iterator<int*,
std::__cxx1998::vector<int, std::allocator<int> > >,
std::__debug::vector<int>, std::random_access_iterator_tag>]
Error: function requires a valid iterator range [__first, __last).
Objects involved in the operation:
iterator "__first" @ 0x000000000023FDB0 {
type = __gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >
(mutable iterator);
state = dereferenceable (start-of-sequence);
references sequence with type 'std::__debug::vector<int, std::allocator<int> >' @ 0x0000000000
23FD10
}
iterator "__last" @ 0x000000000023FD80 {
type = __gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >
(mutable iterator);
state = past-the-end;
references sequence with type 'std::__debug::vector<int, std::allocator<int> >' @ 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.
The only part I cared to look at was Error: function requires a valid iterator range [__first, __last). . 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)!
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++?