简体   繁体   English

std :: list的多元素插入是否强烈异常安全?

[英]Is std::list's multi-element inserts strongly exception-safe?

In item 17 of exceptional c++ , I find this: exceptional c++ 17项中,我发现:

First, for all containers, multi-element inserts ("iterator range" inserts) are never strongly exception-safe. 首先,对于所有容器,多元素插入(“迭代器范围”插入)从不强烈异常安全。

but in item 1 of effective STL , I find this: 但在effective STL 1项中,我发现:

If you need transactional semantics for multiple-element insertions (eg, the range form — see Item 5), you'll want to choose list, because list is the only standard container that offers transactional semantics for multiple-element insertions. 如果您需要多元素插入的事务语义(例如,范围形式 - 请参阅条目5),您将需要选择列表,因为列表是唯一为多元素插入提供事务语义的标准容器。

and in page 249 of the c++ standard library 2th , I find this: the c++ standard library 2th249页,我发现:

For lists, even multiple-element insert operations are transaction safe. 对于列表,即使是多元素插入操作也是事务安全的。

So my question is which one is right? 所以我的问题是哪一个是对的? Is strongly exceptional-safe means the same with transaction safe? 强烈的特殊安全意味着交易安全吗?

  1. which one is right? 哪一个是对的?

For all the overloads of std::list::insert , strongly exception-safety is guaranteed. 对于std::list::insert所有重载,保证了强异常安全性。

Exceptions 例外

If an exception is thrown, there are no effects (strong exception guarantee). 如果抛出异常,则没有效果(强异常保证)。

and from the standard, $23.3.5.4/2 list modifiers [list.modifiers] : 从标准的$23.3.5.4/2 list modifiers [list.modifiers]

If an exception is thrown there are no effects. 如果抛出异常则没有效果。

then 然后

  1. is strongly exceptional-safe means the same with transaction safe? 非常特殊 - 安全意味着与交易安全相同吗?

Yes. 是。 Here 's an explanation from Herb Sutter: 以下是Herb Sutter的解释:

Strong Guarantee: If an exception is thrown, program state remains unchanged. 强保证:如果抛出异常,程序状态保持不变。 This level always implies global commit-or-rollback semantics, including that no references or iterators into a container be invalidated if an operation fails. 此级别始终表示全局提交或回滚语义,包括如果操作失败,则无法使容器中的引用或迭代器失效。

It is already answered that std::list provides this guarantees as per standard. 已经回答说std::list按照标准提供了这种保证。 I'd like to mention why it is possible to do this in the list. 我想提一下为什么可以在列表中执行此操作。

You can provide this guarantee because list has a constant complexity merge operation, which is a non-throwing operation. 您可以提供此保证,因为列表具有常量复杂性合并操作,这是一种非抛出操作。 All you need to do is to first create a temporary list, fill the temporary list with values and than merge temporary list into original list. 您需要做的就是首先创建一个临时列表,用值填充临时列表,然后将临时列表合并到原始列表中。

If exception happens while populating temporary list, nothing is merged, and temporary list is simply disposed when the insert exits. 如果在填充临时列表时发生异常,则不会合并任何内容,并且在插入退出时简单地处理临时列表。

Since no other container provides constant complexity no-throwing merge, it is not possible with any other container. 由于没有其他容器提供持续复杂的无投掷合并,因此任何其他容器都无法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM