c++ - 为什么 string::replace 在 C++11 中不指定分配器的异常?

标签 c++ string exception replace language-lawyer

如果我正确理解文档,std::string::replace甚至可以用更长的字符串替换字符串的某些部分:

std::string s("hello");
s.replace(s.begin() + 1, s.end() - 1, ".....");
std::cout << s;  // prints "h.....o"

如果容量不足以容纳新字符串,则可能需要重新分配。但是,replace 的异常规范在 C++11 标准中只提到了 out_of_rangelength_error异常(exception)。

在当前草案中,分配器的 allocate 抛出了额外指定的异常。成员函数 [string.replace.8.3] :

Throws: ...

any exceptions thrown by allocator_­traits<Allocator>​::​allocate.

我想知道为什么 C++11 中没有指定这些异常?库函数是否可以抛出未在 Throws: 子句中指定的其他异常?

最佳答案

这是一个错误;委员会最近才修复它。一些此类修复被明确称为具有追溯性(,即使在旧语言模式下,实现也应以新方式运行)。这个不是,也许是因为,呃,任何实现都不太可能以任何其他方式表现。

简而言之,不要读太多。

关于c++ - 为什么 string::replace 在 C++11 中不指定分配器的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57749433/

相关文章:

c++ - 内联函数实际上做了什么?

c++ - poco Lib 无法在 vs express 9 中编译

c++ - 将字符串分配给 stack.top() 时出现段错误

c++ - 字符串文字到 basic_string<unsigned char>

ruby-on-rails - AirBrake vs Exceptional vs errbit : which one is the best to track exceptions in your rails app?

java - Mockito 测试抛出异常的 void 方法。 Spring Boot,eclipse IDE

c++ - 插入运算符的非成员函数形式

python - 如何从 Pandas 的 YYYY-YY 格式列中提取去年 (YYYY)

python - 使用 Numba 时如何指定 'string' 数据类型?

java - 当我输入字母 a b c 时,java 中的期望会下降