Adam Petersen - Software Development Pages, Book Reviews Section
Start News Articles Book Reviews

Book Review

The C++ Standard Library Extensions: A Tutorial and Reference
by Pete Becker

ISBN-13: 978-0321412997
Publisher: Addison-Wesley Professional
Pages: 624


With the upcoming version of the new standard, C++ gets a well-needed vitamin injection. The library extensions (currently compiled in a technical report, TR1) ranges from the mandatory (smart pointers), the useful (regular expressions, hashed containers, and numerics) and interesting (call wrappers) to the ignorable (C compatibility with C99 additions). In this book, Pete Becker guides us through the extensions. The part of most interest will depend on your problem domain, but there are some libraries that I believe are genuinely useful in virtually all C++ programs.

Let me start with smart pointers. These are a must for any program handling dynamic memory. Until TR1, the only standard smart pointer was std::auto_ptr. Due to its destructive copy-semantics, std::auto_ptr is limited in its usefulness. Most notably, it isn’t legal to store std::auto_ptrs in containers. The new extensions add a reference counted shared_ptr that solves this problem and an accompanying weak_ptr that holds a pointer to a controlled resource, but doesn’t impact the reference count. A weak_ptr is typically used in circular data structures.

The additions of new call wrappers finally make the existing algorithms (for_each, find_if, etc) in the standard library practically useful. Basically, call wrappers are a workaround for inherent limitations in the core language. Algorithms typically achieve genericity by allowing parameterization with functions (callable types in C++ terms or, less formal, functors). One pain with the C++ object system is that it uses a different syntax for invoking free-standing functions and member functions. The new call wrapper tr1::mem_fn is a workaround for this problem and unifies the syntax. More yet, using tr1::mem_fn the compiler figures out the calling syntax based on object type; that is, it can take an argument of either reference, value or pointer type and figures out behind the scenes how to invoke it. tr1::bind is a more general call wrapper that allows binding functors, member functions, free-standing function, etc, together with additional arguments. Besides enabling currying in C++, tr1::bind together with the tr1::function provides a way to emulate (limited) closures.

While the features I have discussed are mostly workarounds for limitations in C++, they also demonstrate the strength of the language; C++ has a built-in way to grow. Its template mechanism allows new features to be added without changing the core language, a property that’s badly missing in Java to take a prominent example.

Pete Becker provides many interesting insights into these new features, insights that go well beyond the obvious and have been won during his experience with implementing the library extensions. This gives the book an authoritative tone that works great for the reference part. However, as a tutorial I am hesitant to recommend it. Becker provides lots of code examples of very good quality, but in my opinion they fail to demonstrate why you need those new features and how useful they can be. There are simply no interesting examples (Karlsson’s “Beyond the C++ Standard Library” does a much better job in this respect). To be fair, in the chapters on regular expressions the examples are significantly better and more practically oriented. That’s great, because regular expressions are probably the most underused tool in programming and now that C++ finally support them the barrier is lowered.

Unfortunately the code samples have other problems. Most disturbingly the output from the programs isn’t included. For a tutorial and introduction this is disastrous. I also didn’t like the layout. Many code samples are broken across pages, sometimes with just one, single line of code on the next page. This doesn’t exactly ease the understanding of the material.

Despite my criticism, if you are serious with C++ and have some experience with the extensions you want this book. As most of the libraries are available in boost today, the material is immediately useful. For more inexperienced C++ programmers I would recommend to start with Karlsson’s “Beyond the C++ Standard Library” instead.

Reviewed March 2007


©2005 Adam Petersen adam@adampetersen.se