Just reading 21.3.6.1 basic_string::find – and there is no mention that the pos argument be within bounds of the string itself.
21.3.6.1 basic_string::find
size_type find(const basic_string& str,
size_type pos = 0) const;1 Effects: Determines the lowest position xpos, if possible, such that
both of the following conditions obtain:— pos <= xpos and xpos + str.size() <= size();
— at(xpos+I) == str.at(I) for all elements I of the string controlled
by str.2 Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
I am reading this correctly?
The wording of the spec does not require that
posbe less than the length of the string. Ifposis greater than the length of the string, then the first part of condition (1) cannot be satisfied, because ifpos <= xpos, thenxpos + str.size() <= size()can never be true. As a result, by clause (2), the function will returnnpos. Consequently, if the index is out of range, the behavior of the function is still perfectly well-defined.Hope this helps!