This book may be a little bit old (2001), but it’s still very relevant today. A lot of the material in the book is still not applied in C++ development, it may be time to apply it, doesn’t it?
Content and opinions
The book is split in two parts: the basis and the more complex blocks.
For Alexandrescu, there are four basis that should be covered: polycies, type lists, allocators and general techniques. Polycies is tightly related to the strategy pattern. The author discusses the elegant and complex architecture that can be written using them. Type lists is, in my opinion, the first step in using metaprogramming. Since Alexandrescu wrote this book, the Boost library implemented a lot of tools to work on type lists (as well as different types of type lists). This book explains very well how it can work and how to make the most of it. Allocators is perhaps one of the toughest topics in C++. Fortunately, the book provides a good implementation for small objects, and it is stable and efficient. I don’t think it is sound to implement your own, so using this one is perhaps best! Finally, the second chapter tackles different small tricks that can be easily implemented in every C++ code.
The second part is much more complex. Using the different basis blocks from the first part, Alexandrescu starts by creating a generic functor. It’s perhaps too much for 99% of the projects, but it still is a great proof of concept for elegant C++ architecture. The second topic in this part is singletons. Alexandrescu tries to address every side of this question, and although he provides an implementation, it is unfortunately not yet available/used in every framework. Next come smart pointers. They will be available in the next C++ standard, but only in a reduced form than what is exposed here. It is interesting to know the different issues that such a “simple” class has to solve. The next two chapters are dedicated to factories and abstract factories. As usual, one can create its own factory, but Alexandrescu tries to solve the more general problem. Before my favorite chapter, implementing the visitor pattern is addressed. It’s not the most widely used pattern, but it solves some tricky cases. Finally, even less used is the multiple dispatch function. Imagine you have to instanciate several functions with different types. You wan use type lists for this if you have only one type for each function. But if you have a more general case with two or three types that have to be instanciated, you have to use the multiple dispatch. It can be found in the intersection sample that is used in the book, but also in steering code (your favorite template class can be used with objects that can be of different types, like int matrices or float matrices, and you only have access to the base class of this type, and the class instance must call the correct instanciation).
Each time, the main focus is on automation, less code, more readability. Of course, this last point is only achieved if the reader has some knowledge of this modern C++ design…
Alexandrescu wrote Loki as a proof of concept of modern C++. The book is the result of the different solutions he thought of. Loki may be outdate by Boost in several fields, but it is also the foundations of the Boost library. If you want to write good professional-grade C++, you should read at least once this book and appropriate its content.
[amazon template=iframe image&asin=0201704315]