Re: Is there any standard/guarantees for exception safety in STL operations?
Michael Kilburn wrote:
On Jul 18, 3:29 pm, Daniel Kr?gler <daniel.krueg...@googlemail.com>
wrote:
I have checked C++ standard and did not find requirements imposed
on library implementation with respect to exception safety.
...
There exist some very general statements and guarantees, see below.
Yes, I saw them... the problem is that quite often they are not
"very general" -- they are incomplete! I'll quote an example from
those provided by you:
5) 23.1/10:
"Unless otherwise specified (see 23.2.1.3 and 23.2.4.3) all
container types defined in this clause meet the following
additional requirements:
? if an exception is thrown by an insert() function while
inserting a single element, that function has no effects.
... and what if we are inserting more than one element? what
effect(s) we are going to have?
Bad probably. :-)
You could also read this as a hint to not insert multiple obejcts at
once, if they are likely to throw exceptions.
As an application programmer, you might know that there are plenty of
resources available so your classes will not throw right now.
? no swap() function throws an exception unless that exception
is thrown by the copy constructor or assignment operator of
the container?s Compare object (if any; see 23.1.2).[..]"
... and what is going to happen to the object?
The Compare object is provided by you, the programmer. This rule can
be read as: "If you take care of the Compare object not throwing, the
library guarantees the rest of the container."
It is extremely unusual for compare objects to throw during a swap.
Instead of outright forbidding it, the library instead warns you that
throwing is not good. Perhaps you, as a programmer, can determine if
this particular compare object for this particular container is likely
to throw or not during the next swap.
If it is safe, go ahead and swap the container. If not, perhaps you
can avoid the swap? Or live with the consequences?
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]