What does the C++11 standard specify for the behavior of the
string& erase (size_t pos = 0, size_t len = npos); member function when the
pos argument is passed as
string::npos? I would think it should erase nothing, but perhaps it throws an
out_of_range exception instead? What is the defined behavior for the standard?
std::out_of_range, as specifically stated in the standard:
22.214.171.124 basic_string::erase [string::erase]
basic_string& erase(size_type pos = 0, size_type n = npos);
pos <= size()
pos > size().
Effects: Determines the effective length
xlenof the string to be removed as the
size() - pos. The function then replaces the string controlled by
*thiswith a string of length
size() - xlenwhose first
poselements are a copy of the initial elements of the original string controlled by
*this, and whose remaining elements are a copy of the elements of the original string controlled by
*thisbeginning at position
pos + xlen.