Title: Modern C++ Design Author: Andrei Alexandrescu Publisher: Addison Wesley Professional, 2001 Pages: 352 ISBN: 0201704315 Price: $39.95 Anyone who thinks theyve seen it all in C++, or that theres nothing new left to be said, obviously hasnt read Modern C++ Design.
Modern C++ Design is an important book. Fundamentally, it demonstrates how to create reusable design pattern implementations in C++ a new way to combine templates and patterns that you may never have dreamt was possible, but is. Modern C++ Design is not for novices. There are a lot of advanced techniques here, but its a testament to Alexandrescus skill that he teaches advanced and useful design techniques in a way thats easy to understand, assimilate, and put into practice on your next project. Better still, you can reuse the code in the books Loki library to solve real-world problems right away with a few caveats that are due, not to Loki itself, but just to limitations in some of todays compilers.
The Book
If your work involves production C++ design and coding, you should read this book. Even if you only read the first two chapters, you will come away with important ideas about how to design for extensibility in C++ and many useful supporting techniques based mainly on templates. But fair warning: if you do read the first two chapters, youll probably be hooked and wont be able to stop until you reach the back cover. Thats a good thing, because although the first couple of chapters are cool and interesting, the real meat and value of the book comes later on.
Weve probably all written Singleton and Visitor in our code. Whats more, the ugly truth is that weve all done it many times. This is because, as the patterns community is quick to remind us, design patterns are not code. Patterns, they are equally quick to elaborate, merely document recurring styles and idioms that crop up in the design process, which then only later eventually manifest as code. The pattern description tells you what the pattern looks like, but then its up to you to go write new code every time you want to apply it.
Wouldnt it be nice if, instead, patterns could be made available as reusable chunks of code you could call directly from your program, like std::list or std::sort? Arent you tired yet of writing Singleton and Visitor over and over? (If not, why not?) And so the main value and meat of Modern C++ Design lies in its techniques for creating just that: reusable implementations of design patterns, actual nuts-and-bolts code libraries than can be reused directly without hand-implementing them every time. Such generic patterns, or pattern templates as I think of them, are a powerful new way of creating extensible designs in C++. To demonstrate, Alexandrescu provides walkthroughs for several specific generic pattern implementations, devoting one chapter each to Abstract Factory, Multimethod, Smart Pointer, Singleton, and Visitor.
But wait, I can hear someone object, thats not how it works in practice! In the real world, theres no one Singleton; theres no one Visitor. We implement those patterns in different ways in different places even in the same program, depending on the exact requirements in each place theyre needed! I just dont believe that there can be any useful one-size-fits-all solution. (For example, consider the classic and thorny question of when a Singleton should be destroyed; the answer may be different for different Singletons in your program.)
That objection is perfectly valid, and I can imagine Alexandrescu smiling and rubbing his hands gleefully and responding: Yup. And Im glad you raised that question, because I was just getting to that part.... Such variability, as it turns out, is no barrier to Alexandrescus approach: he simply writes all (well, all right then, many) of the versions of Singleton youll ever need. You then instantiate the reusable Singleton implementation with policies that make it work the way you need in that particular place in your code and instantiate it with different policies in other places. What if none of the default policies does quite what you want? Chances are you can write or specialize your own policy that will, thus writing the minimum code once, and only once, to extend and customize just that part of the pattern implementations behavior to account for the new requirement. The point is that the implementation of Singleton really is done once up front and then reused many times, instead of being implemented by hand in every place. No fuss, no muss.
Indeed, let me put it more succinctly still: implementation variability is not a barrier to Alexandrescus approach; its the very reason his approach is important.
Oh, and he does the same with smart pointers. And Visitor. And Multimethods. Natty stuff.
An obligatory but minor caveat: the first printing of Modern C++ Design unfortunately contained numerous (literally hundreds of) typographical errors. I hasten to stress that these are not the authors fault, but simply unfortunate production problems, and the typos are mostly obvious and dont affect the quality of the material in the book. I also hasten to stress that this is probably of historical interest only, because by the time you read this review, youll almost certainly be buying the second or later printing, which pretty much have fixed these errors wholesale.
The Loki Library
Modern C++ Design describes the techniques and facilities available as the Loki library, which is freely available, downloadable, and pretty much ready to use. If you want to try out some or all of Lokis facilities, including Singleton, Visitor, and others, you can do it today.
One of the things Ive noticed that makes Loki so interesting is that can compiler XYZ compile Loki? is becoming something of an unofficial benchmark in compiler-writer circles. Dont get me wrong, Loki is written entirely in normal Standard-conforming C++ only more standard and more conforming, it turns out, than some compilers are yet quite ready to digest. Loki is one of the most technically advanced C++ libraries available, which is a polite way of saying that it uses templates so widely and heavily that it tears the tar and stresses the stuffing out of some current compilers. In fact, at the time the book was released, no commercially available compiler could compile all of Loki correctly, despite notes to the contrary in the books Preface about compilers that could compile most of Loki. But progress is being made: recently, Loki has been ported to the Gnu g++ platform, with only minor tweaks to work around miscellaneous spots of compiler indigestion.
Its a bit ironic that Loki is deliberately named after the Norse god of wit and mischief. The name was designed to highlight the playful flexibility of the library. It has an unintentional second application: Loki wreaks more mischief with todays compilers than does any other popular library.
Summary
Although this book is definitely not for novices, its hard to overestimate the impact of Andrei Alexandrescus Modern C++ Design. Indeed, if Jim Coplien hadnt already written an influential book called Advanced C++ Programming Styles and Idioms, Im sure thats what the title for Alexandrescus book could easily and correctly have been. I highly recommend this book for the serious C++ practitioner.
Herb Sutter (<www.gotw.ca>) is an independent consultant and one of the instructors of The C++ Seminar (<www.gotw.ca/cpp_seminar>). He is also secretary of the ISO/ANSI C++ standards committee and author of the acclaimed books Exceptional C++ and More Exceptional C++.