Re: The C++ article in April issue of DDJ
On Mar 20, 7:01 pm, "Andrei Iltchenko" <andrei.ilche...@gmail.com>
wrote:
On Mar 20, 12:10 am, Greg Herlihy <gre...@pacbell.net> wrote:
In this case the base class, std::runtime_error's, has a destructor with the
most restrictive exception specification possible (a throw() clause
indicating that it throws no exceptions of any type). Therefore
StreamingException's destructor - in order to have an exception
specification at least as restrictive as the destructor of its base class
that it overrides - must also also be declared with a throw() specification.
That's all true and I wouldn't argue with this. My point is that he
need not have overriden the destructor. The author chose to provide
the user-defined destructor for the sole purpose of ensuring that it
has an empty exception specification claiming that an implicitly-
declared destructor wouldn't have an empty exception specification,
which is wrong.
According to the standard. I've had problems with this with
specific compilers, and like the author, always provide a user
defined destructor in such cases.
[...]
BTW, there's another small problem with the class. The
evaluation of the 'new std::stringstream()' expression in the
member initializer of the default constructor can result in
the bad_alloc exception being thrown, and exception classes'
constructors shouldn't throw.
Why not? It's generally preferable that the copy constructor
not throw (but his didn't), but requiring a guarantee that the
expression given as argument to the keyword throw won't itself
generate an exception doesn't seem reasonable to me.
If there is a problem with the class, it is that his
implementation of what() may throw (although he has declared it
throw()). In practice, of course, it shouldn't be called until
the stack has been unwound, freeing up resources, and so
probably will never throw in practice (even if he didn't have
any more memory available at the throw point). Still, a better
solution would probably be to use a temporary at the throw
point, to collect the stream, then throw in its destructor,
after having allocated a char[] in which to put the string (and
managing the char[] by means of a boost::shared_array, if for
some reason he is not using the Boehm collector).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]