Article View: comp.std.c++
Article #4056Re: exceptions in std lib
From: Nathan Myers ncm
Date: Tue, 16 Apr 1996 06:10
Date: Tue, 16 Apr 1996 06:10
58 lines
3542 bytes
3542 bytes
James Kanze US/ESC 60/3/141 #40763 wrote: > > In article <316F646D.552DF1DD@cantrip.org> "Nathan Myers > <ncm@cantrip.org>" <ncm@cantrip.org> writes: > |> Bad things happen if you throw during copy construction or destruction > |> of a vector element. > Does this imply that `vector< string >' is unsafe. That's debatable. I think the Library WG expects that most implementations will guarantee that copying strings doesn't throw, though no such a guarantee is required by the standard. It *does* imply that for `vector< deque<int> >', operations that require reallocation in the vector could trigger exceptions when copying the deque<int> container objects, leaving the vector in an undefined state -- that is, it's not safe to call the destructor. For a vector on the stack, this would be pretty bad: you would not be able to recover, or terminate the program normally. > |> Don't let it happen. (Yes, I know this is easier > |> said than done. Still.) There are lots of things in the library that > |> could break if a user object (or even a standard object!) throws during > |> a standard function. > If I understand what you are saying correctly, we should not use > exceptions with the standard library, because it will not be required > to be exception safe. I'm just talking about the current state of the Draft. Yes, it is as bad as it sounds, and will be until somebody figures out what to do about it and writes up a sound proposal. Before you criticize this, consider what you would expect vector<> to do when it is copying a block of elements from one memory region to another, or deleting a block of elements, and an exception happens in the middle. Do you stop there, half-done? Do you copy or delete the rest of the elements and leave a hole, then rethrow? (What if you get another exception while doing the rest?) If you propagate the exception, member functions of the vector (including the destructor) will probably be called later. What will they find? This is a long way of asking: What do you mean by `required to be exception-safe'? If you can define it, then probably the standard library ought to meet the definition. If you *can't* define it, then (at least) you will then need to be careful about what you put in a vector. Pointers & PODs are OK. Nathan Myers ncm@cantrip.org [ comp.std.c++ is moderated. To submit articles: try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ] [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ] [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<317326BE.246CF449@cantrip.org>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<9604121530.AA08606@sun132.spd.dsccc.com> <316F646D.552DF1DD@cantrip.org> <KANZE.96Apr15210506@slsvgqt.lts.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: "Nathan Myers <ncm@cantrip.org>" <ncm@cantrip.org> Newsgroups: comp.std.c++ Subject: Re: exceptions in std lib Date: 16 Apr 1996 06:10:03 GMT Organization: Best Internet Communications Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <317326BE.246CF449@cantrip.org> References: <9604121530.AA08606@sun132.spd.dsccc.com> <316F646D.552DF1DD@cantrip.org> <KANZE.96Apr15210506@slsvgqt.lts.sel.alcatel.de> NNTP-Posting-Host: taumet.eng.sun.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nntp-Posting-Host: ncm.vip.best.com X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i386) Content-Length: 2343 Originator: clamage@taumet