在哪里可以找到标准容器和算法的所有异常保证? [英] Where can I find all the exception guarantees for the Standard Containers and Algorithms?

查看:249
本文介绍了在哪里可以找到标准容器和算法的所有异常保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我看过 C ++标准(或者草稿),但是我没有发现STL容器提供的任何全面的异常保证。我可以找到的是有些某些类型的功能的部分不完整的说明。或者也许它在那里,但我只是没有找到它,我不知道。



注意:我是 em>不 要求人们可以想到的所有保证的清单,这基本上是在这个问题

我正在寻找这个信息的权威来源本身 - 或者最好是源的免费版本(例如标准草案),我可以或多或少地将其视为官方的。

解决方案

阅读标准可能是吓人的(我们回到标准),但是Bjarne Stroustrup在他的书C ++编程语言中写了一个非常好的附录。他发布本附录



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



它很长,详细(写得很好)。您可以找到E.4部分有趣的话题:


E.4标准集装箱保证



如果库操作本身引发异常,它可以 - 并且 -
确保其运行的对象留在
中定义状态。例如,对于
向量(§16.3.3),throw()抛出out_of_range并不是向量
的异常安全性问题。 at()的作者没有问题,确保向量在抛出之前处于
明确的状态。


另外,E.4.1节说明了


除基本保证外,标准库还提供
的强大保证一些插入或删除元素的操作。


看看第956页。它包含一个表的保证,用于矢量的各种操作,deque,列表和地图。
总而言之,这些容器上的所有操作都不是强或不强,除了 N元素插入映射中提供基本保证。

注意:上面的文本是旧的,并没有解决C ++ 11,但是对于大多数目标和目的来说,它们仍然是足够正确的。



当涉及到C ++ 11 ...



标准首先说明关于容器
数组,deque,forward_list,list,vector,map,set,unordered_map,unordered_set,queue,stack
at



23.2.1 / 10


除非另有说明(见23.2。 4.1,23.2.5.1,23.3.3.4和
23.3.6.5)本条款中定义的所有容器类型满足以下附加要求:



- 如果异常被insert()或emplace()函数抛出,而
插入一个元素,该函数没有任何效果。

- 如果push_back()或push_front( )函数,
这个函数没有任何效果。

- 没有erase(),clear(),pop_back()或pop_front()函数抛出
异常。 b $ b - 没有返回的迭代器
的复制构造函数或赋值运算符抛出异常。

- 否sw ap()函数抛出一个异常。

- 没有swap()函数使任何引用,指针或
迭代器无效,引用被交换的容器的元素。


上面提到的各个部分(每个称为异常安全保证)中的怪癖主要是关于特殊的对墙的情况,例如处理包含的类型的散列,比较操作以及投掷交换和投掷操作。


Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the functions for some of the types. Or perhaps it's there but I'm just not finding it, I don't know.

Note: I'm not asking for a list of all the guarantees people can think of, which is basically in this question.
I'm looking for the authoritative source of this information itself -- or preferably, a free version of the source (e.g. a draft of the standard) where I can more or less treat as official.

解决方案

Reading the standard can be scary (let's come back to the standard), but Bjarne Stroustrup has written a really nice appendix on this subject in his book 'The C++ Programming Language'. He posted this appendix at

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

It's pretty long and detailed (and well written). You may for example find section E.4 interesting, quote:

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.

In addition, section E.4.1 states

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

have a look at page 956. It contains a table of guarantees for various operations for vector, deque, list and map. In summary, all operations on those containers are either nothrow or strong, except for N - element insert into map which offers the basic guarantees.

Note: the above text is old and does not address C++11, but should still be correct enough for most aims and purposes.

When it comes to C++11...

the standard first states, about the containers array, deque, forward_list, list, vector, map, set, unordered_map, unordered_set, queue,stack: at

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.

The quirks pointed out in the respective sections referred to above (each called Exception safety guarantees) are mostly about special against-the-wall cases like when dealing with exceptions from the contained types' hashing, comparison operations as well as throwing swap and throwing move operations.

这篇关于在哪里可以找到标准容器和算法的所有异常保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆