Search code examples
c++stringerase

string::erase(0) on an empty string?


Is the behaviour of std::string::erase(0) is well defined on an empty string. Because cppreference says:

Removes count characters starting at index.

But for an empty string, the character at index 0 does not exist.


Solution

  • It seems to be OK, since the size of the string is 0:

    21.4.6.5 basic_string::erase [string::erase]

    basic_string<charT,traits,Allocator>& erase(size_type pos = 0, size_type n = npos);

    1 Requires: pos <= size()

    2 Throws: out_of_range if pos > size().