c++ - 我在哪里可以找到标准容器和算法的所有异常保证?

标签 c++ exception standard-library c++-faq exception-safety

是的,我看过 C++ standards我可以找到(或草稿),但我没有找到任何全面的 STL 容器提供的异常保证。我能找到的只是偶尔出现的部分,其中对 some 类型的 some 功能的描述不完整。或者它在那里,但我只是没有找到它,我不知道。

注意:不是要一份人们能想到的所有保证的 list ,基本上在this question .
我正在寻找此信息本身的权威来源 - 或者最好是我可以或多或少视为官方的来源的免费版本(例如标准草案)。 p>

最佳答案

阅读标准可能会令人恐惧(让我们回到标准),但 Bjarne Stroustrup 在他的《C++ 编程语言》一书中就这个主题写了一个非常好的附录。他将此附录发布在

http://www.stroustrup.com/3rd_safe0.html , 在 http://www.stroustrup.com/3rd_safe.pdf

它很长很详细(而且写得很好)。例如,您可能会发现第 E.4 节很有趣,引用:

E.4 Standard Container Guarantees

If a library operation itself throws an exception, it can – and does – make sure that the objects on which it operates are left in a well-defined state. For example, at() throwing out_of_range for a vector (§16.3.3) is not a problem with exception safety for the vector . The writer of at() has no problem making sure that a vector is in a well-defined state before throwing.

此外,第 E.4.1 节指出

In addition to the basic guarantee, the standard library offers the strong guarantee for a few operations that insert or remove elements.

看看第 956 页。它包含一个保证 vector 、双端队列、列表和映射的各种操作的表。 总而言之,除了提供基本保证的N - element insert into map 之外,对这些容器的所有操作都是 nothrow 或 strong。

注意:上面的文字是旧的,不涉及 C++11,但对于大多数目标和目的来说应该仍然足够正确。

说到 C++11...

标准首先声明,关于容器 array、deque、forward_list、list、vector、map、set、unordered_map、unordered_set、queue、stack: 在

23.2.1/10:

Unless otherwise specified (see 23.2.4.1, 23.2.5.1, 23.3.3.4, and 23.3.6.5) all container types defined in this Clause meet the following additional requirements:

— if an exception is thrown by an insert() or emplace() function while inserting a single element, that function has no effects.
— if an exception is thrown by a push_back() or push_front() function, that function has no effects.
— no erase(), clear(), pop_back() or pop_front() function throws an exception.
— no copy constructor or assignment operator of a returned iterator throws an exception.
— no swap() function throws an exception.
— no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.

在上面提到的各个部分中指出的怪癖(每个都称为异常安全保证)主要是关于特殊的撞墙情况,例如在处理来自包含类型的散列、比较操作以及抛出交换的异常时和 throw 移动操作。

关于c++ - 我在哪里可以找到标准容器和算法的所有异常保证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11699083/

相关文章:

c++ - "STL allocate memory internally"是什么意思?

abstract - Agda 标准库 - 为什么更多属性没有标记为抽象?

c++ - 将标准容器传递给函数

c++ - 在 C++ 中重载 operator new 和 operator new[] 有什么区别?

c++ - 用于 boost 多索引容器的模板参数

vb.net - 如何在 VB.NET 中引发异常

php - 传递无效参数类型时显示异常消息

c++ - 如何编写一个完美的缩写函数模板?

java - 当年龄不在范围内时抛出用户定义的异常

c++ - seekg 和 seekp:seek(streampos pos) 和 seek(streamoff off, ios::beg) 之间的区别