Consider standard algorithms like, say, std::for_each.
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f);
As far as I can tell, there is actually no requirement placed on the relative states of the two InputIterator arguments.
Does that mean that the following is technically valid? Or is it undefined? What can I realistically expect it to do?
std::vector<int> v{0,1,2,3,4};
std::for_each(
v.begin()+3, // range [3,0)
v.begin(),
[](int){}
);
geordi tells me:
error: function requires a valid iterator range [__first, __last). [+ 13 discarded lines]
but I can’t tell how compliant this debug diagnostic is.
I came up with this question when trying to pedantically determine how explicit is defined the behaviour of the following:
std::vector<int> v; // <-- empty
std::for_each( // <-- total no-op? stated or just left to implication?
v.begin(),
v.end(),
[](int){}
);
The standard explicitly requires the
lastiterator to be reachable from thefirstiterator. That means that by incrementingfirstone should be able to eventually hitlast.