Article View: comp.std.c++
Article #2572Re: Exceptions in destructors
From: Anatoli Tubman
Date: Tue, 14 Nov 1995 18:52
Date: Tue, 14 Nov 1995 18:52
67 lines
3659 bytes
3659 bytes
******************* MODERATOR'S ATTENTION PLEASE **************************
I have never posted to a Usenet newsgroup before. I don't even have access
to NNTP, so I cannot read news on a regular basis. So it may well happen
that I'm doing something wrong. If this is the case, can you please email me
your suggestions?
***************************************************************************
[ moderator's note: You did it right. You can post to comp.std.c++
and it will be forwarded automatically to a moderator, or you can
email the article to the "submission address" below. -sdc ]
James Kanze <kanze@lts.sel.alcatel.de> writes:
> 1. Given:
> T a[ 5 ] ;
> When destructing the third element (for example), an exception is
> thrown. Are the remaining elements destructed? I would say yes
Unfortunately, DWP is not clear enough in this area.
Here what it says (the numbers of statements are mine):
(1) An object that is partially constructed will have destructors executed
only for its fully constructed sub-objects.
(2) Should a constructor for an element of an automatic array throw an
exception, only the constructed elements of that array will be
destroyed.
(3) If the object or array was allocated in a new-expression, the storage
occupied by that object is sometimes deleted also.
(Note that (3) does not appear in the ARM.)
I think that (2) should be rewritten, perhaps like this:
(2*) An object becomes fully constructed when its constructor returns
and remains such until a destructor for it is invoked.
This covers the case covered by the present wording, and many others, for
which DWP makes no provisions -- without violating the spirit.
Also, (3) needs to be a little bit more specific. I think the following
will do well:
(3*) A delete-expression always deletes storage allocated for its operand,
regardless of any exceptions that might be thrown by any of the
destructors involved.
If an implementation does not impose an added cost to code that does not
catch or throw exceptions, it can be easily modified to comply with the
above rules and still add no overhead to such code.
Whether throwing from destructors shoud ever be considered by the C++ user
(as opposed to implementor) is a totally different question.
I believe that inability to destroy what you've just created
is a symptom of some very serious problem, for which terminate() is an
appropriate solution. However, standards should not prohibit
things only because they are ``bad style'' -- there must be other reasons.
-----------------------------------------------------------------
Anatoli Tubman <anatoli@ptc.com>
DISCLAIMER: My opinions are not necessarily those of my employer
or any other person or entity.
-----------------------------------------------------------------
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<Pine.SGI.3.91-heb-2.02.951114164854.2393A-100000@arava>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: Anatoli Tubman <anatoli@ptc.com> Newsgroups: comp.std.c++ Subject: Re: Exceptions in destructors Date: 14 Nov 1995 18:52:42 GMT Organization: ? Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <Pine.SGI.3.91-heb-2.02.951114164854.2393A-100000@arava> Reply-To: anatoli@ptc.com NNTP-Posting-Host: taumet.eng.sun.com Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset="US-ASCII" X-Lines: 59 Content-Length: 2666 Originator: clamage@taumet