Re: Is exception-safety possible at all?
The clause Adam cited states that exceptions are possible *unless*
*otherwise* *specified*. I regard 17.3.1.3/3 in combination with the
absence of a "Throws" description in the function semantics to fulfil
the "otherwise specified" part.
If that is not good enough, then all discussions about how to achieve
exception safety are essentially moot, because then even a cornerstone
algorithm as std::swap does not give any guarantee about exception
safety.
For example, std::swap() does not have a "Throws" description, so it
should not throw anything by itself, but it might propagate exceptions.
Possible sources for these exceptions could be the default constructor,
copy-constructor and/or copy-assignment operator of the template
parameter.
This does not presume a particular implementation of std::swap. For all
I know, it could be implemented as a couple of memcpy() calls and a
temporary array of char.
The only thing I presume is that the implementation does not perform
(fallible) actions that are not needed to meet the semantics of the
function.
Yes. std::swap does not give guarantee of non-throwing. It does not
even give conditional guarantee of non-throwing in case copy
constructor and assignment operator do not throw. You have already
shown a conforming implementation which might throw due to lack of
memory in swap itself (not in copy constructor and assignment
operator).
This is even worse. David Abrahams stated that operations on base
types do not throw. In such case code like:
void my_swap( int& left, int& right )
{
const int temp = left;
left = right;
right = temp;
}
will not throw as well. However std::swap<int> might throw (in a
conforming implementation). And aren't functions like swap basis of
exception safety, especially the strong one?
Adam Badura
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]