I l@ve RuBoard Previous Section Next Section

Exception-Safety Issues and Techniques

First, a little of the history of this topic is in order. In 1994, Tom Cargill published the seminal article "Exception Handling: A False Sense of Security" (Cargill94).[1] It demonstrated conclusively that, at that time, the C++ community did not yet fully understand how to write exception-safe code. In fact, we didn't even know what all the important exception-safety issues were, or how to correctly reason about exception safety. Cargill challenged anyone to demonstrate a conclusive solution to this problem. Three years passed. A few people wrote partial responses to aspects of Cargill's example, but no one showed a comprehensive solution.

[1] Available online at http://www.gotw.ca/publications/xc++/sm_effective.htm.

Then, in 1997, Guru of the Week #8 appeared on the Internet newsgroup comp.lang.c++.moderated. Number 8 generated weeks of discussion and presented the first complete solution to Cargill's challenge. Later that year, a greatly expanded version that was updated to match the latest changes to draft standard C++ and demonstrating no fewer than three complete solutions, was published in the September and November/December issues of C++ Report under the title "Exception-Safe Generic Containers." (Copies of those original articles will also appear in the forthcoming book C++ Gems II [Martin00].)

In early 1999, on the Effective C++ CD (Meyers99), Scott Meyers included a recombined version of the articles, together with Cargill's original challenge, with the updated text of his classic books Effective C++ and More Effective C++.

This miniseries has come a long way since its original publication as Guru of the Week #8. I hope you enjoy it and find it useful. Particular thanks go to fellow committee members Dave Abrahams and Greg Colvin for their insights into how to reason about exception-safety and their thoughtful critiques of several drafts of this material. Dave and Greg are, with Matt Austern, the authors of the two complete committee proposals for adding the current exception-safety guarantees into the standard library.

This miniseries tackles both major features, exception handling and templates, at once, by examining how to write exception-safe (works properly in the presence of exceptions) and exception-neutral (propagates all exceptions to the caller) generic containers. That's easy enough to say, but it's no mean feat.

So come on in, join the fun, and try your hand at implementing a simple container (a Stack that users can push and pop) and see the issues involved with making it exception-safe and exception-neutral.

    I l@ve RuBoard Previous Section Next Section