Re: exception specifications in the standard library
on Tue Apr 10 2007, Andreas Schmitt <keldorkatarn-AT-gmx.de> wrote:
stl container members don't seem to have any exception specifications,
also the same for many other standard library features.
The containers are a special case. It is simply nearly impossible to
make a container exception safe
I beg to differ. Every conforming implementation of C++ has
exception-safe containers, for any reasonable definition of the term
"exception-safe."
for one simple reason: You do not know anything about the type that
is going to be put into the container. That type could throw
anything from runtime_error to user defined unknown
exceptions. Catching all possible, even unknown exceptions inside
the container any processing them in a way that keeps the container
in a no-throw state is very hard if not impossible. That is why most
containers do not have any exception specifications. You simply
cannot easily guarantee anything for a type you are using but do not
know anything about.
Not really. There are container operations that are guaranteed not to
throw, and those could have had exception specifications.
The simple fact of the matter is that the standard is haphazard in its
use of throw() to indicate "can't throw anything." Except on a
virtual function, the as-if rule says the appearance of "throw()" in
the specification does not oblige implementations to actually use it.
throw() is used in some places, and in others, the standard says
"throws: nothing." They are -- except in the case of virtual
functions -- equivalent statements. By the time the committee was
ready to do anything disciplined about exception handling in the
standard library, it was too late to make trivial changes like using
one consistent way of saying "doesn't throw," so the inconsistencies
remain as an hysterical artifact.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
Don't Miss BoostCon 2007! ==> http://www.boostcon.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]