The standard library containers allow us to erase
ranges denoted by iterators first
and last
.
std::vector<foo> bar;
// first it last it
bar.erase(bar.begin(), bar.end());
The standard says that the first
iterator must be valid and dereferenceable, whereas last
only needs to be valid. However if first == last
then first
does not need to be dereferenceable because the erase
is a no-op. This means the following is legal:
bar.erase(bar.end(), bar.end());
However if I only wish to erase one element rather than a range the iterator must be valid and dereferenceable making the following undefined behaviour:
bar.erase(bar.end());
Why isn't this just a no-op? Is it an oversight by the standards committee that will be addressed in a future revision of the language or is it a deliberate design decision that I'm not seeing the point of?
As far as I can see it provides no benefits but creates extra headaches when performing something like the following:
bar.erase(std::find(bar.begin(), bar.end(), thing));