std::list 的多元素插入是否具有很强的异常安全性?
Is std::list's multi-element inserts strongly exception-safe?
在 exceptional c++
的第 17 项中,我找到了这个:
First, for all containers, multi-element inserts ("iterator range"
inserts) are never strongly exception-safe.
但在 effective STL
的第 1 项中,我发现:
If you need transactional semantics for multiple-element insertions
(e.g., 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.
在 the c++ standard library 2th
的第 249
页中,我发现:
For lists, even multiple-element insert operations are transaction safe.
所以我的问题是哪一个是正确的?强异常安全和事务安全是一样的吗?
- which one is right?
对于 std::list::insert
的所有重载,都保证了强烈的异常安全性。
Exceptions
If an exception is thrown, there are no effects (strong exception guarantee).
根据标准,.3.5.4/2 list modifiers [list.modifiers]
:
If an exception is thrown there are no effects.
然后
- is strongly exceptional-safe means the same with transaction safe?
是的。 Here 来自 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.
已经回答 std::list
按照标准提供此保证。我想在列表中提及 为什么 可以这样做。
你可以提供这个保证,因为list有一个常复杂度的合并操作,这是一个非抛出操作。您需要做的就是首先创建一个临时列表,用值填充临时列表,然后将临时列表合并到原始列表中。
如果在填充临时列表时发生异常,则不会合并任何内容,并且在插入退出时简单地处理临时列表。
由于没有其他容器提供恒定的复杂性不抛出合并,因此任何其他容器都不可能。
在 exceptional c++
的第 17 项中,我找到了这个:
First, for all containers, multi-element inserts ("iterator range" inserts) are never strongly exception-safe.
但在 effective STL
的第 1 项中,我发现:
If you need transactional semantics for multiple-element insertions (e.g., 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.
在 the c++ standard library 2th
的第 249
页中,我发现:
For lists, even multiple-element insert operations are transaction safe.
所以我的问题是哪一个是正确的?强异常安全和事务安全是一样的吗?
- which one is right?
对于 std::list::insert
的所有重载,都保证了强烈的异常安全性。
Exceptions
If an exception is thrown, there are no effects (strong exception guarantee).
根据标准,.3.5.4/2 list modifiers [list.modifiers]
:
If an exception is thrown there are no effects.
然后
- is strongly exceptional-safe means the same with transaction safe?
是的。 Here 来自 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.
已经回答 std::list
按照标准提供此保证。我想在列表中提及 为什么 可以这样做。
你可以提供这个保证,因为list有一个常复杂度的合并操作,这是一个非抛出操作。您需要做的就是首先创建一个临时列表,用值填充临时列表,然后将临时列表合并到原始列表中。
如果在填充临时列表时发生异常,则不会合并任何内容,并且在插入退出时简单地处理临时列表。
由于没有其他容器提供恒定的复杂性不抛出合并,因此任何其他容器都不可能。