Exceptional C++ Style

Dr. Dobb's Journal January, 2005

By Matthew Wilson

Matthew is the creator of the STLSoft libraries, and author of Imperfect C++ (Addison-Wesley, 2004). Matthew can be contacted at matthew@synesis.com.au.

Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions
Herb Sutter
Addison-Wesley Professional, 2004
352 pp., $39.99
ISBN 0201760428

Exceptional C++ Style is the third book from Herb Sutter, and the third in the Exceptional C++ series, following the eponymous Exceptional C++ and its sequel More Exceptional C++.

I should say upfront that Exceptional C++ is my favorite C++ book. When the book came out, it was like someone switched on a light on post-C++-98 C++ practice. It had the clarity of writing of Scott Meyers, (almost) the efficiency of Bjarne Stroustrup, and accessibility to difficult issues previously unseen. Although I've read a lot of highly recommendable books since then, none has matched that impact.

Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions starts off well with a look at (ab)uses of std::vector. As usual, Sutter manages to tease out abuses that you'd probably never dream of, but that are instructive nonetheless. If you're like me, your craft is moderated and governed by experience, and lessons learned quickly moved out of your consciousness into your subconscious instincts and style. I think one of the values of the Exceptional C++ books is that they bring such lessons to the fore. Either Sutter gets to see a lot of bad code, or he's able to put himself into mindsets that practicing programmers work hard to leave behind. Whichever, it's a great foundation for his educational writings.

Items 2 and 3 concern themselves with sprintf(), snprintf(), string streams, and Boost's lexical_cast as measures for converting from different types to strings. The material is largely up to scruff, but I had a problem with the broad-brush statements that sprintf() is unsafe/insecure, rather than identifying the actual causes of its susceptibility to overruns, such as being asked to translate types that are themselves of variable length. After all, there's absolutely nothing unsafe about converting any integral type to a string of 21 or more characters by sprintf(). I also thought it a bit too kind of Sutter not to object more strenuously to the poor levels of performance he'd ascertained for some of the more complex solutions. That's the kind of thing that gives C++ a bad name for being overly complex and inefficient.

Item 4 discussed member function adaptors, and gave me another justification for my antipathy to default parameters. Item 6 covers a common misapprehension with respect to pointers and iterators and the evil old operator &. It also offers a nice guideline to the effect that if your type has a swap() method, then you should overload std::swap() for it. This book is full of these golden nuggets.

Item 7 discusses the ramifications of function templates, specializations, and overloading. This is essential template survival guide stuff. Item 8's coverage of techniques for befriending templates leaves you yearning for better compilers.

Items 9 and 10 are about export. Here I had some real conflicts. The explanation given of export is nothing short of wonderful. However, Sutter sets you up to form your own conclusion that export is the most colossal white elephant in programming history, without actually making the point himself. I think he is wrong to leave you dangling so. Whatever the imagined virtues at the time of standardization (1995-98), to have something so unbelievably complex (as Sutter says, it took the finest template/compiler minds longer to make export work than to implement Java) in the language standard, which only one compiler currently provides, is nothing but perfect ammunition for C++'s detractors.

Being about exceptions, Items 11-13 are classic Exceptional C++ and truly exceptional. C++ writing doesn't get any better than this! Item 14 covers virtual inheritance and construction order and, although it curiously fails to discuss diamond inheritance, was a good refresher; I found it odd to be forced to think about virtual inheritance, having not used it in years.

Item 16 is brilliant. While it didn't actually teach me anything per se, it enhanced and clarified both my experiential understanding, and cleared my terminology. Above all, this is what I value most in Sutter's writings. The same goes for Item 17. Item 18, which describes the NonVirtual Interface pattern, is also good, although it missed one advantage of NVI—how it clears up the default argument confusion you tend to get into with virtual methods.

The remainder of the book covers a variety of subjects, including memory, optimization, odd little puzzles, and style issues. It also takes a few swipes at std::basic_string, which is fair play in my book! There are a few dodgy ones, but the bulk of them are enlightening, entertaining, and educational.

Is Exceptional C++ Style as good as Exceptional C++? Maybe not. But it is a very good book, and you'll do yourself a disservice if you don't rush out and get a copy straight away.

DDJ