Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why string::replace does not specify allocator's exceptions in C++11?

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?

like image 495
Daniel Langr Avatar asked May 21 '26 01:05

Daniel Langr


1 Answers

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.

like image 50
Davis Herring Avatar answered May 22 '26 16:05

Davis Herring