Every so often, a book comes along which changes the whole way you view programming: Stroustrup, Design Patterns, and Scott Meyer’s Effective C++ and More Effective C++ are all widely regarded as classic programming texts. And now a new volume joins these exalted tomes: Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu.
Existing books on C++ design have skipped lightly over templates and generic programming, perhaps mentioning them briefly as a useful way of creating containers for arbitrary types (as in the STL, for example). What Alexandrescu presents here is, effectively, template programming from another dimension: policy programming (using templates to genericise class functionality, not just the types they operate upon), compile-time manipulation of collections of types using template metaprogramming, compile-time assertions, generalised functors (any C++ processing invocation, encapsulated in a typesafe first-class object), template template parameters (not a typo – using templates as parameters to templates to incredible effect) and much, much more.
But this is more than just a clever set of techniques. What Alexandrescu is presenting here is nothing short of a whole new way of approaching C++ development, one in which generics aren’t simply a convenient way of cutting down on the number of linked list classes you have to write – they’re a fundamental and massively powerful way to genericise functionality in your code.
In some ways, it’s depressing to read books like this, because within the first 12 pages you will have had to throw away everything you thought you knew about OO design (how any of this stuff maps to UML, for example, I have no idea) and start all over again. But in the end, it’s worth it, simply because the ideas presented here are just so goddamned clever. Quite simply astonishing, and one of the most significant books on C++ I’ve ever read – if you’re a C++ developer, you need to read this book; it’s that simple.