128 of 137 people found the following review helpful
on April 7, 2004
Read this book and you may feel you've fallen down the hole into some sort of C++ Alice-in-Wonderland:
"Isn't it nifty just how much you can do with functions... that not only don't do anything but don't even really exist at all?" p.36
The title is a bit presumptuous; a more accurate title would be "Template Metaprogramming Stunts", since this book is primarily about tricks you can play with C++'s template mechanism.
The author does also make a go at proposing a new concept of "policy-based class design" -- by which he means using templates intead of multiple inheritance to create combinatorial mixtures of behavior. This is interesting, but seems hard to apply beyond the cliches he considers (ex: smart pointers), so his argument reads like a well-intended but parochial graduate thesis, with dutiful gestures of respect to his mentors (ex: Scot Myers) and limited range of real-world application.
The real meat of the book is his template techniques, which are ingenious -- if perverse, when seen from the standpoint of someone else trying to read and extend your code, or diagnose its arcane compiler errors. If you've ever had to work with other people on software, you may find his glib view of compiler errors disappointing. In many cases he relies on compiler errors to signal something more complex and semantic than the mere error would suggest. (If you're programmed much C++ you've surely experienced 10-line long STL template errors with a very simple underlying cause that is hard to discern from the error message. Learning to suss out what these mean is much of the sweat-work of learning C++.) I think this is simply that he's coming from the perspective that the interworkings of the language and the compiler are fascinating, and worth being a primary focus of study. But from the perspective of someone just tried to get your work done in the problem domain at hand, you may resent this attitude.
This book is best approached if you think C++ is really neat. Wheter you love C++ for its roccoco intricacy, or just see it as an evolutionary leaf node language full of botched misfeatures, you will find much here to confirm your opinion. I don't mean to belittle this book. There is real insight here: the idea of computing on types instead of values (to store the results of template-based computations) is a nifty idea. But it's worth keeping an eye on what problems you are really trying to solve, and which ones you may be creating.
Some telling quotes:
"Unfortunately, type_info's useful capabilities are packaged in a way that makes them unnecessarily hard to exploit." p.38
"For a variety of reasons, C++ is a language that leads its users sometimes to say, 'These are the smartest five lines of code I ever wrote.' Maybe it is its semantic richness or the ever-exciting (and surprising?) way its features interact." p.51
140 of 158 people found the following review helpful
on July 14, 2001
While this is an interesting book, I'm not as unconditionally enthusiastic about it as most of the reviewers here seem to be. I felt that, as opposed to Scott Meyers' books and the original _Design Patterns_ book, many of the techniques presented in this book are the result of research into "elegant" solutions rather than experience with what really works in practical applications.
I really liked chapter 1 discussing policy-based class design, chapter 2 discussing various template techniques, chapter 6 discussing singleton implementation, anbd chapter 7 discussing smart pointers. Chapter 11 discussing multimethods, appears to be a good discussion of various alternatives but I'm not inclined to use multimethods anytime soon.
Chapter 3 about typelists is interesting, but after all the dazzling manipulations, all that typelists seem to be good for is generating class hierarchies, which I didn't find very compelling - it looks as if the typelists help to manage complexity that possibly wouldn't be there if the typelists hadn't been used in the first place.
Chapter 4 about small object allocation was of limited interest to me. The other chapters, 5 (Functors), 8 (Object Factories), 9 (Abstract Factory), and 10 (Visitor) offered fairly complex, template based solutions that to me did not look obviously superior to the straightforward manual implementations shown in _Design Patterns_.
Overall, a stimulating book, but one whose recipes will need careful evaluation before use.
51 of 55 people found the following review helpful
on May 24, 2001
I was longing for a good book on advanced C++. First, I bought "Multi-Paradigm C++". It does have some insights but author's style is baroque to the extreme. A good cure for insomnia. Then I bought "Generative Programming". I found it too much of a PhD thesis garnished with too much fluff for my taste.
Not wanting to risk anymore, I borrowed this book from a friend... to lose sleep for two nights reading it! It is the book I was looking for: no-nonsense, eye-opening, inspirational. The author's mastery of C++ and the creativity with which he solves real-world problems are phenomenal.
Alexandrescu discusses and implements the following design patterns: Command, Singleton, Proxy (in the form of smart pointers), Factory Method, Abstract Factory, and Visitor. In doing so, he creates highly configurable components that support most variations that accompany the patterns, and allow the component users to add their own variations. This is done through template parameters. The technique is not new but the author documents it thoroughly (coining the Policy-based design term) and uses it very creatively to achieve elegant designs.
The book also has foundation chapters: Techniques, Typelists, and a small object allocator. The latter is not really in sync with the rest of the book, being a low-level component written in straight C++, but in compensation, the chapter on typelists is as meta as you can imagine, and more :-).
In short: a great book if you use C++, especially so if you need to participate in the architectural stage of projects. But the best thing the book has done for me is, it made me extremely excited about programming - a feeling forgotten since my college days. Five stars plus.
34 of 37 people found the following review helpful
on April 2, 2001
I was waiting for this, and pestering bookstores for it for 2 weeks before it arrived. And it still exceeded my expectations. Note: The author was partly responsible for the C++ ScopeGuard library - an amazing, essential piece of code.
For any C++ engineers using reference counting (that'll be all of us, then), the amazingly configurable SmartPtr<> and the SmallObjectAllocator are instantly worth many times the price of the book. The deep, thorough analysis of Singletons, Functors, the Visitor pattern, Double-dispatch and Factories are all important chapters in their own right, with immediate use in real programs as well as educational value. The pervading concept of policy classes will undoubtedly influence your future approach to design, because the examples are all so elegant, compelling and downright useful.
But I think there is even more value in the longer term. This book takes a somewhat dizzying step into a new way of thinking, reminiscent of first encountering STL or GoF's Design Patterns. I feel sure that the concept of typelists is an important contribution to the direction of the language; a flexible, generic, *declarative* way of expressing relationships between abstractions. However, these techniques place such strain on the C++ template facility (which even in its final ISO form is barely adequate for the task) that it's amazing that they work at all. The book is full of ingenious work-arounds to overcome language limitations.
I just wish that more current compilers would accept the code. Sadly, Visual C++ 6.0 is a long way from this level of standard-conformance. Hopefully VC++ .NET will be better.
28 of 30 people found the following review helpful
on July 12, 2001
I highly recommended this book if you are interested in generic techniques and Design Patterns
This book uses C++ and Generic programming techniques to implement GoF Design Patterns . It introduces Andrei's "Generic Design Patterns Library", called "Loki". Loki's techniques are more important than the library itself and should be applicable in many other contexts. (Since the library is not yet available, I cannot comment on it further.) One word of caution (lest you get overly excited) - Loki uses some of the latest C++ features and requires standard-adhering compilers [g++ v2.95.2-5, Borland C++ v5.5, and EDG's v2.45 or later. Microsoft's VC++ 6.0 fails-Ed.]
The book is written for intermediate or expert level programmers. A fair knowledge of C++, particularly templates, is needed Also, some knowledge of Design Patterns is useful, although not strictly necessary, since the book describes the patterns in a pretty self-contained fashion.
With these prerequisites the book makes for quite an enjoyable read, but I would not call it easy due to the inherently complexity of the material covered. Andrei's writing style is frank and friendly, which makes the reading easier.
I found the code fragments to be very accurate bar some mechanical/typesetting errors, some of which will be fixed by the time of the printing. The book's strong points include:
1) Taking techniques as complex as template meta-programming and bringing them back to us earthly beings. You could get a more extensive treatment of these techniques from the book Generative Programming  or from Todd Veldhuisen's papers  but the examples in this book make a very good introduction to the area.
2) Creating a generic library of design patterns using "policy-based" classes eliminate the redundancy found in many systems that utilize Design Patterns.
3) The one line of the book I'd like to (mis?)quote in my review is "Remember, the design constraints buried in the class's design are as bad as magic constants embedded in code". As obvious as it is, this principle is often overlooked (I know...) and largely because people do not see a solution to it and are willing to live with the "this is as good as it gets" mentality. In this sense the book was a great eye-opener - it showed the feasibility of the solutions and the techniques for the implementation.
Personally, I hope you enjoy the book as much as I did!
37 of 41 people found the following review helpful
on August 1, 2002
Simply put, this is one of the best C++ books I have come across. It will open your eyes to what generic programming really is and how it can be done with C++.
As another review states, "this book is mostly about templates". Certainly true. This book takes templates where few have dared venture. Rather than use templates as a glorifed macro system, Alexandrescu uses templates to define small code generation engines. Your knowledge of C++ will most certainly be expanded, but you'll also come away from this book with a different way of looking at programming problems.
The book also picks apart many popular design patterns and shows how to implement them in very generic ways. Alexandrescu's treatment of certain patterns is so thorough that you'll understand the pattern better from this book than the original.
Perhaps the techniques go too far, perhaps there is a limit to how generic one should go. That is yours to decide, but this book is definitely a keeper for the C++/software development bookshelf.
72 of 84 people found the following review helpful
on August 15, 2005
With about 13 years of C++ programming and 8 years of standard C before that I can say that this book, while useful as an idea guide for what is possible with C++ template metaprogramming, I would recommend against utilizing these techniques in production code.
Most template meta-programs are candidates for programming obfuscation contests, and when you get into programming in the large, the situation is aggravated.
For instance, one example in the book is an elaborate class-registration/class-factory scheme implemented with a template metaprogram. Consider as an alternative the class-registration and factory in the Metrowerks PowerPlant class library. PowerPlant, which is extraordinarily well designed, achieves the same functionality with a single template function (no metaprogramming required) in conjunction with the rigor of exposing a single public constructor (function signature 'pattern') in each class you wish to register with 'the registrar'.
Template programs are notorious for their tendency to tie your code to a single compiler/vendor (see the Boost.org compatibility chart). My advice, use templates judiciously and when the problem can only otherwise be expressed using the macro preprocessor.
The best book on template programming has a black cover and is about 2" thick (softcover). It's called "Generative Programming: Methods, Tools and Applications" by Krzysztof Czarnecki et al. (ISBN: 0201309777). This book will mold your thinking into new ways of architecting large software systems using features of C++, including templates, in novel ways that actually reduce complexity, make more maintainable code and utilize templates in novel and thought-provoking ways.
I refactored a complex software project making extensive use of C++ templates, and I employed techniques from Generative Programming. I used none of the techniques from Modern C++ Design.
If you are a manager and your technical team wants to use the techniques from Modern C++ Design, make sure they sign on for at least 5 years of maintainance and all the bug fixes. That oughta sober them up a bit. The techniques illustrated in this book are essentially unmaintainable with the current generation of C++ compilers and the error messages they emit.
Templates were designed to replace the #define preprocessor techniques that were nearly impossible to understand and maintain. If your template code is harder to maintain than your most elaborate #define code, then I say you're moving in the wrong direction.
21 of 22 people found the following review helpful
on June 4, 2001
First came type-safe containers and generic algorithms, and then came traits. Now, thanks to Andrei Alexandrescu's ground breaking new book "Modern C++ Design," we have policies. Policies provide a means of creating very flexible and extendable libraries with little to no run-time overhead.
As an example consider the Singleton pattern. What is the lifetime of the singleton? Should it be destroyed? If so, when? What, if any, threading model should the used by the singleton? Unfortunately, there is no one best answer. The good news is that policies allow the application developer to choose the answer that best fits their needs. Several policies are presented in the book but best of all is the fact that if none of the policies are well suited to the problem at hand, it is remarkably easy to write a new policy that plugs into the library.
While it may be tempting to write an entire book on singletons, the book covers a myriad of other topics, including but not limited to, smart pointers, object factories, abstract factories, visitors, and a memory allocator optimized for small objects. Each topic is covered thoroughly and is easy to read and understand. Be forwarded however, the reader must have of strong understanding of C++ template fundamentals. This is not an introductory tutorial on templates.
There are a number of books that should be considered "must have" for all professional C++ programmers and designers. These books include Scott Meyers' "Effective C++" and "More Effective C++", Herb Sutter's "Exceptional C++", and the "Gang of Four's" "Design Patterns." "Modern C++ Design" is the newest addition to this list.
32 of 36 people found the following review helpful
on January 4, 2002
The techniques employed in Loki (the library presented in the book) are fairly solid directions for the future of C++ programming, but the reality of today is that many of these techniques rely heavily on mechanisms (templates, STL) that aren't well implemented in current compilers.
This is probably the best available book of applications for template metaprogramming, which is a frightening abuse of C++ templates and lookup rules to perform computation at compile time. It does have it's place, particularly the simpler tricks like compile time assertions.
A primary thrust of design is the use of policy classes, which are template template parameters that generally end up as base classes. This use of empty multiple inheritance base classes comes with a cost that the author wasn't aware of when he wrote the book. A discussion to that effect appeared in comp.std.c++ with the title "Obese compound objects??? >:o(".
The SmartPtr discussion is one of the best I've ever seen (and Scott Meyers taught a class on that topic here at work). However, the book doesn't cover every little detail of the Loki implementation, which has vague comments like "Colvin/Gibbons trick." I suppose accu.org might have a pointer to what that means, but I feel this sort of detail should have been covered all in one place.
All that said, it's still a very welcome addition to any C++ programmer's library, but in my library it's a reference among many of trendy C++ abuses that might turn up in somebody else's code I may have to work with some day.
20 of 22 people found the following review helpful
on May 1, 2001
Rob Pike once said: "Narrowness in experience leads to narrowness in imagination." Andrei's book astonishes me every other page I read, showing me how to do things I couldn't even imagine were possible in C++. Did I know it was possible to assert on a condition evaluated at compile time, similar to what I used to do at run-time? Pass a list of types as an argument to a function? Customize the storage, conversion, and checking policies of a smart pointer class? Not only I found out they were attainable, but I have learned how to combine and use them to create more flexible powerful abstractions, such as memory allocators, callable entities, dispatch engines, object and class factories. This book nicely fills in a gap between the books comprehensively covering language features and standard library components and those addressing elements of software design. The techniques presented in the book are techniques that scale well and can be used in large software systems. Actually, the larger the system, the better the reusable components described in the book can be used, and the greater the benefits. As the compilers' support for the C++ standard constantly improves with each new release, I expect these components to become portable soon. Even though the presentation has some problems (typos, style, and using a constant-width font for the code snippets), I find the reading of this book to be quite relaxing and enjoyable. If I recommend it? Definitely! I'm cutting my teeth on it...