If I understand the documentation correctly, std::string::replace may replace some part of a string even with a longer string:
std::string s("hello");
s.replace(s.begin() + 1, s.end() - 1, ".....");
std::cout << s; // prints "h.....o"
This might require reallocation if the capacity is not high enough for a new string. However, the exception specification for replace in the C++11 Standard does mention only out_of_range and length_error exceptions.
In the current draft, there are additionally specified exceptions thrown by the allocator's allocate member function [string.replace.8.3]:
Throws: ...
— any exceptions thrown by
allocator_traits<Allocator>::allocate.
I wonder why these exceptions are not specified in C++11? May library functions throw additional exceptions not specified in the Throws: clause?
It was a bug; the committee fixed it only recently. Some such fixes are explicitly called out as being retroactive (i.e., implementations are expected to behave the new way even in old language modes). This one wasn’t, perhaps because it’s, er, highly unlikely that any implementation ever behaved any other way.
In short, don’t read too much into it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With