Consider standard algorithms like, say, std::for_each
.
template
Function for_each(InputIterator first
This is explained by section 24.1 of the standard, "Iterator Requirements":
An iterator
j
is called reachable from an iteratori
if and only if there is a finite sequence of applications of the expression++i
that makesi == j
. Ifj
is reachable fromi
, they refer to the same container.…
Range
[i, j)
is valid if and only ifj
is reachable fromi
. The result of the application of functions in the library to invalid ranges is undefined.
So v.begin() + 3
is reachable from v.begin()
, but not the reverse. So [v.begin()+3, v.begin())
is not a valid range, and your call to for_each
is undefined.