Top positive review
20 people found this helpful
on 2 March 2007
This is 300 pages of the hardest of hardcore template metaprogramming you could currently hope to read. As a result, there is a risk you may find more to admire and speculate upon from afar, than to actually use. Various factors will decide this, from the robustness of your compiler, your comfort with far-out template wackiness, and your confidence in debugging and maintaining the result.
One thing is not in doubt: C++ metaprogramming is devilishly clever. That's part of its appeal. It's also capable of producing flexible, efficient families of software. Modern C++ Design pushes the boundaries of TMP further than the more specialised matrix domain detailed in Czarnecki and Eisenecker's Generative Programming (a recommended prologue to this book). Here, the aim is implementing entire design patterns. This is a fairly bold claim, as the received wisdom on design patterns is that there isn't a canonical form that can be presented in code.
And that still holds true. Instead, the code in Modern C++ Design is metacode built on two big ideas: policies and typelists, which are described in the opening chapters, along with some other useful tools. The hard intellectual work is done here, especially the typelist chapter, although previous exposure to the likes of Generative Programming definitely softens the blow. Policies are what Java programmers know as Dependency Injection, except one level of abstraction up (the relationship between classes and objects in DI is done with templates and classes with policies), and with the template instantiation and specialisation rules of C++ providing an extra layer of flexibility. Typelists are exactly as the name suggests - a list of types, but they exist only at compile time. They're fed to the TMP machinery, which spits out a class hierarchy based on the supplied list.
With these tools in place, Andrei Alexandrescu then proceeds to show how they can be used to work some magic. Template versions of the command, visitor, abstract factory and factory method patterns are presented, along with other abstractions like smart pointers (perhaps the most orthodox chapter of the book), tuples, and multiple dispatch.
The latter topic provides a connection to James Coplien's Advanced C++, which also discussed multimethods, albeit before templates entered the language. The books share a certain similarity, not so much in content, but in the spirit of stretching the boundaries of C++, and perhaps also in stretching its readability and maintainability.
For all the virtuosity on display here (and in Coplien's book), I'm not sure if I'd to use these techniques in my own C++. This may just be down to my own mediocrity as a programmer, but TMP is more than just an idiomatic application of C++, it's really an entirely new language and not a very friendly one. When encoding error messages into class names is a best practice, you know you're pushing the envelope.
There's no discussion in this book of where these designs are appropriate - I suspect the most rococo code will end up hidden safely behind frameworks and libraries such as the Loki library described in this book. Some discussion of these bigger picture strategic and cultural issues would have been handy, otherwise, a title like 'Modern C++ Design' suggests that herein lie techniques to sprinkle liberally throughout your code, and I suspect that this is not the case.
If you, your co-workers, and any customers that need to look at your code are all template metagenii, this is going to be the most productive 300 pages you ever read. Lesser mortals will find much to wonder at and admire, and a whole new perspective on design patterns, but perhaps less to put into practice.