6

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.

Vincent
  • 57,703
  • 61
  • 205
  • 388

2 Answers2

9

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().

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

On the same page of std::string::erase I found these lines:

Exceptions
1)     std::out_of_range if index > size().
2-3) (none)

David G
  • 94,763
  • 41
  • 167
  • 253
Anand Rathi
  • 790
  • 4
  • 11