Consider the following:
std::vector<int> vec(1); // vector has one element
std::fill(vec.begin(), vec.begin(), 42);
std::fill(vec.begin()+1, vec.end(), 43);
std::fill(vec.end(), vec.end(), 44);
Will all of the std::fill usages above result in defined behavior? Am I guaranteed that vec will remain unmodified? I’m inclined to think “yes”, but I want to make sure the standard allows such usage.
No, if doesn’t cause undefined behavior.
The standard defines empty iterator range in 24.1/7 and nowhere it says that supplying an empty range to
std::fillalgorithm causes undefined behavior.This is actually what one would expect from a well-thought through implementation. With algorithms that handle emtpy range naturally, imposing the requirement to check for empty range on the caller would be a serious design error.