Search code examples
c++insertlanguage-lawyerstdlistexception-safety

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


In item 17 of exceptional c++, I find this:

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:

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.

and in page 249 of the c++ standard library 2th, I find this:

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?


Solution

    1. which one is right?

    For all the overloads of std::list::insert, strongly exception-safety is guaranteed.

    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]:

    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:

    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.