Modern C++ Design: Generic Programming and Design Pattern... and over one million other books are available for Amazon Kindle. Learn more

Vous voulez voir cette page en français ? Cliquez ici.

Sign in to turn on 1-Click ordering.
Amazon Prime Free Trial required. Sign up when you check out. Learn More
More Buying Choices
Have one to sell? Sell yours here
Start reading Modern C++ Design on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Modern C++ Design: Generic Programming and Design Patterns Applied [Paperback]

Andrei Alexandrescu
4.5 out of 5 stars  See all reviews (55 customer reviews)
List Price: CDN$ 67.99
Price: CDN$ 42.83 & FREE Shipping. Details
You Save: CDN$ 25.16 (37%)
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
Only 6 left in stock (more on the way).
Ships from and sold by Gift-wrap available.
Want it delivered Wednesday, September 3? Choose One-Day Shipping at checkout.


Amazon Price New from Used from
Kindle Edition CDN $33.06  
Tankobon Hardcover --  
Paperback CDN $42.83  
Save Up to 90% on Textbooks
Hit the books in's Textbook Store and save up to 90% on used textbooks and 35% on new textbooks. Learn more.
Join Amazon Student in Canada

Book Description

Feb. 13 2001 0201704315 978-0201704310 1
Achieve more with C++ than you ever imagined possible!
Introduces generic components, which offer breakthrough power for maximizing expressiveness, flexibility, and reuse of code.
Readers will learn exciting, powerful new C++ idioms that will help them master the most modern library writing techniques.
Forewords by Scott Meyers (Effective C++), one of the worlds leading C++ experts, and by John Vlissides, of the IBM T.J. Watson Research center, one of the worlds leading patterns experts. In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and virtuosity, Alexandrescu offers a cutting-edge approach to software design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code. The book introduces the concept of generic components, reusable design templates that enable an easier and more seamless transition from design to application code, generate code that better expresses the original design intention, and support the reuse of design structures with minimal recoding. The author then shows how to apply this approach to recurring, real-world issues that C++ programmers face in their day-to-day activity. All code is available on the Web, along with Alexandrescus downloadable Loki C++ library, which provides powerful out-of-the-box functionality for virtually any C++ project. For experienced C++ programmers who have at least some familiarity with the Standard Template Library (STL).

Special Offers and Product Promotions

  • Join Amazon Student in Canada

Frequently Bought Together

Modern C++ Design: Generic Programming and Design Patterns Applied + The C++ Programming Language (4th Edition) + Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)
Price For All Three: CDN$ 125.34

Customers Who Bought This Item Also Bought

Product Details

Product Description

From the Inside Flap

You might be holding this book in a bookstore, asking yourself whether you should buy it. Or maybe you are in your employers library, wondering whether you should invest time in reading it. I know you dont have time, so Ill cut to the chase. If you have ever asked yourself how to write higher-level programs in C++, how to cope with the avalanche of irrelevant details that plague even the cleanest design, or how to build reusable components that you dont have to hack into each time you take them to your next application, then this book is for you.

Imagine the following scenario. You come from a design meeting with a couple of printed diagrams, scribbled with your annotations. Okay, the event type passed between these objects is not char anymore; its int. You change one line of code. The smart pointers to Widget are too slow; they should go unchecked. You change one line of code. The object factory needs to support the new Gadget class just added by another department. You change one line of code.

You changed the design. Compile. Link. Done.

Well, there is something wrong with this scenario, isnt there? A much more likely scenario is this: You come from the meeting in a hurry because you have a pile of work to do. You fire a global search. You perform surgery on code. You add code. You introduce bugs. You remove the bugs . . . thats the way a programmers job is, right? Although this book cannot possibly promise you the first scenario, it is nonetheless a resolute step in that direction. It tries to present C++ as a newly discovered language for software architects.

Traditionally, code is the most detailed and intricate aspect of a software system. Historically, in spite of various levels of language support for design methodologies (such as object orientation), a significant gap persisted between the blueprints of a program and its code because the code must take care of the ultimate details of the implementation and of many ancillary tasks. The intent of the design is, more often than not, dissolved in a sea of quirks.

This book presents a collection of reusable design artifacts, called generic components, together with the techniques that make them possible. These generic components bring their users the well-known benefits of libraries, but in the broader space of system architecture. The coding techniques and the implementations provided focus on tasks and issues that traditionally fall in the area of design, activities usually done before coding. Because of their high level, generic components make it possible to map intricate architectures to code in unusually expressive, terse, and easy-to-maintain ways.

Three elements are reunited here: design patterns, generic programming, and C++. These elements are combined to achieve a very high rate of reuse, both horizontally and vertically. On the horizontal dimension, a small amount of library code implements a combinatorialand essentially open-endednumber of structures and behaviors. On the vertical dimension, the generality of these components makes them applicable to a vast range of programs.

This book owes much to design patterns, powerful solutions to ever-recurring problems in object-oriented development. Design patterns are distilled pieces of good designrecipes for sound, reusable solutions to problems that can be encountered in manycontexts. Design patterns concentrate on providing a suggestive lexicon for designs to be conveyed. They describe the problem, a time-proven solution with its variants, and the consequences of choosing each variant of that solution. Design patterns go above and beyond anything a programming language, no matter how advanced, could possibly express. By following and combining certain design patterns, the components presented in this book tend to address a large category of concrete problems.

Generic programming is a paradigm that focuses on abstracting types to a narrow collection of functional requirements and on implementing algorithms in terms of these requirements. Because algorithms define a strict and narrow interface to the types they operate on, the same algorithm can be used against a wide collection of types. The implementations in this book use generic programming techniques to achieve a minimal commitment to specificity, extraordinary terseness, and efficiency that rivals carefully handcrafted code.

C++ is the only implementation tool used in this book. You will not find in this book code that implements nifty windowing systems, complex networking libraries, or clever logging mechanisms. Instead, you will find the fundamental components that make it easy to implement all of the above, and much more. C++ has the breadth necessary to make this possible. Its underlying C memory model ensures raw performance, its support for polymorphism enables object-oriented techniques, and its templates unleash an incredible code generation machine. Templates pervade all the code in the book because they allow close cooperation between the user and the library. The user of the library literally controls he way code is generated, in ways constrained by the library. The role of a generic component library is to allow user-specified types and behaviors to be combined with generic components in a sound design. Because of the static nature of the technique used, errors in mixing and matching the appropriate pieces are usually caught during compile time.

This books manifest intent is to create generic componentspreimplemented pieces of design whose main characteristics are flexibility, versatility, and ease of use. Generic components do not form a framework. In fact, their approach is complementarywhereas a framework defines interdependent classes to foster a specific object model, generic components are lightweight design artifacts that are independent of each other, yet can be mixed and matched freely. They can be of great help in implementing frameworks.


The intended audience of this book falls into two main categories. The first category is that of experienced C++ programmers who want to master the most modern library writing techniques. The book presents new, powerful C++ idioms that have surprising capabilities, some of which werent even thought possible. These idioms are of great help in writing high-level libraries. Intermediate C++ programmers who want to go a step further will certainly find the book useful, too, especially if they invest a bit of perseverance. Although pretty hard-core C++ code is sometimes presented, it is thoroughly explained.

The second category consists of busy programmers who need to get the job done without undergoing a steep learning investment. They can skim the most intricate details of implementation and concentrate on using the provided library. Each chapter has an introductory explanation and ends with a Quick Facts section. Programmers will find these features a useful reference in understanding and using the components. The components can be understood in isolation, are very powerful yet safe, and are a joy to use.

You need to have a solid working experience with C++ and, above all, the desire to learn more. A degree of familiarity with templates and the Standard Template Library (STL) is desirable.

Having an acquaintance with design patterns (Gamma et al. 1995) is recommended but not mandatory. The patterns and idioms applied in the book are described in detail. However, this book is not a pattern bookit does not attempt to treat patterns in full generality. Because patterns are presented from the pragmatic standpoint of a library writer, even readers interested mostly in patterns may find the perspective refreshing, if constrained.


The book describes an actual C++ library called Loki. Loki is the god of wit and mischief in Norse mythology, and the authors hope is that the librarys originality and flexibility will remind readers of the playful Norse god. All the elements of the library live in the namespace Loki. The namespace is not mentioned in the coding examples because it would have unnecessarily increased indentation and the size of the examples.This, alas, means that many current compilers cannot cope with parts of it. I implemented and tested Loki using Metrowerks CodeWarrior Pro 6.0 and Comeau C++ 4.2.38, both on Windows. It is likely that KAI C++ wouldnt have any problem with the code, either. As vendors release new, better compiler versions, you will be able to exploit everything Loki has to offer.

Lokis code and the code samples presented throughout the book use a popular coding standard originated by Herb Sutter. Im sure you will pick it up easily. In a nutshell,

Classes, functions, and enumerated types look LikeThis.

Variables and enumerated values look likeThis.

Member variables look likeThis_.

Template parameters are declared with class if they can be only a user-defined type, and with typename if they can also be a primitive type.


The book consists of two major parts: techniques and components. Part I (Chapters 1 to 4) describes the C++ techniques that are used in generic programming and in particular in building generic components. A host of C++-specific features and techniques are presented: policy-based design, partial template specialization, typelists, local classes, and more. You may want to read this part sequentially and return to specific sections for reference.

Part II builds on the foundation established in Part I by implementing a number of generic components. These are not toy examples; they are components of industrial strength used in real-world applications. Recurring issues that C++ developers face in their day-to-day activity, such as smart pointers, object factories, and functor objects, are discussed in depth and implemented in a generic way. The text presents implementations starting from the needs, the fundamental problems. Instead of explaining what a body of code does, the approach of the book is to discuss problems, take design decisions, and implement those decisions gradually.

Chapter 1 presents policiesa C++ idiom that helps in creating flexible designs.

Chapter 2 discusses general C++ techniques related to generic programming.

Chapter 3 implements typelists, which are powerful type manipulation structures.

Chapter 4 introduces an important ancillary tool: a small-object allocator.

Chapter 5 introduces the concept of generalized functors, useful in designs that use the Command design pattern.

Chapter 6 describes Singleton objects.

Chapter 7 discusses and implements smart pointers.

Chapter 8 describes generic object factories.

Chapter 9 treats the Abstract Factory design pattern and provides implementations of it.

Chapter 10 implements several variations of the Visitor design pattern in a generic manner.

Chapter 11 implements several multimethod engines, solutions that foster various trade-offs.

The design themes cover many important situations that C++ programmers have to cope with on a regular basis. I personally consider object factories (Chapter 8) a cornerstone of virtually any quality polymorphic design. Also, smart pointers (Chapter 7) are an important component of many C++ applications, small and large. Generalized functors (Chapter 5) have an incredibly broad range of applications. Once you have generalized functors, many complicated design problems become very simple. The other, more specialized, generic components, such as Visitor (Chapter 10) or multimethods (Chapter 11) have important niche applications and stretch the boundaries of language support.



From the Back Cover

Modern C++ Design is an important book. Fundamentally, it demonstrates ‘generic patterns’ or ‘pattern templates’ as a powerful new way of creating extensible designs in C++–a new way to combine templates and patterns that you may never have dreamt was possible, but is. If your work involves C++ design and coding, you should read this book. Highly recommended.
–Herb Sutter

What’s left to say about C++ that hasn’t already been said? Plenty, it turns out.
–From the Foreword by John Vlissides

In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code.

This book introduces the concept of generic components–reusable design templates that produce boilerplate code for compiler consumption–all within C++. Generic components enable an easier and more seamless transition from design to application code, generate code that better expresses the original design intention, and support the reuse of design structures with minimal recoding.

The author describes the specific C++ techniques and features that are used in building generic components and goes on to implement industrial strength generic components for real-world applications. Recurring issues that C++ developers face in their day-to-day activity are discussed in depth and implemented in a generic way. These include:

  • Policy-based design for flexibility
  • Partial template specialization
  • Typelists–powerful type manipulation structures
  • Patterns such as Visitor, Singleton, Command, and Factories
  • Multi-method engines

For each generic component, the book presents the fundamental problems and design options, and finally implements a generic solution.

In addition, an accompanying Web site,, makes the code implementations available for the generic components in the book and provides a free, downloadable C++ library, called Loki, created by the author. Loki provides out-of-the-box functionality for virtually any C++ project.

Get a value-added service! Try out all the examples from this book at CodeSaw is a free online learning tool that allows you to experiment with live code from your book right in your browser.


Inside This Book (Learn More)
Browse Sample Pages
Front Cover | Copyright | Table of Contents | Excerpt | Index
Search inside this book:

What Other Items Do Customers Buy After Viewing This Item?

Customer Reviews

Most helpful customer reviews
2 of 2 people found the following review helpful
I read a lot of programming books (maybe too much sometimes... :-) and I think that I am getting very hard to impress but I must confess that this book is very special. It is one of the rare book that did really open my eyes on new possibilities for programming. What the author is doing with templates are things that I have never imagined possible.

The basic concepts presented in this book are policy based class design mixed with some template metaprogramming tricks. The rest of the book consist of revisiting classic design patterns such as smart pointers, singletons and factories by implementing them by using policies and template metaprogramming. The result is, to my opinion, impressive. Implementing these classes may at some point add some complexity but customizing the end result classes is incredibly easy, flexible and requires minimal changes.

Some people did not like the book by arguing that they were not sure if they would be using the patterns presented in the book. I agree with their position and I am not sure that I would find a usage for all the patterns presented in the book or if I would just not want them in my code by fear of the added complexity but in my opinion this is not a good criteria to judge the book. It is not important to know if you are going to use the presented patterns or not. What reading this book will do for sure is to widening the horizon of what you know possible with C++ templates and bring new ideas on how some programming problems can be solved. How many C++ books can do that? For myself, they can be counted with one hand. If you retain the general concepts, it is not important to know if you will use the details.
Was this review helpful to you?
3.0 out of 5 stars Good book but.. May 5 2004
By A Customer
I have to say that this is indeed a good book but it as it share of problems.
The chapter about type list is good, the functor is very good and its multimethods is greate. The reset is between nice to poor. Is implemention of singleton is simply poor programming in both design and implemetion. He seem to fall to any trap posible on the way to impelementing it. The small object allocator is also very poor. The object factory is redandnet as the impemention is C++ is very trivial. The first 2 chapers are nice but if you into template there are other books much better then this (and this book don't intend to teach you about it anyway). The smart pointer is also more of a techenics that you may consider - if you want good staff about it then go to . There are better code about this staff and others anyway. Anther problem is that he seem to come from the windows world and so Loki would not work on any other platfrom without you writing the code to port it - Anyway is design of multithreading is anther shortcoming. This book is good mainly - like the design pattern book - for some insites and idias, other then that the book is of very limiting use in the real world as it trys to solve problems that he cannot solve (unless you design your code around it and not the other way around). I have to say that when I first read the book is seem like its something great and new, but once I read more into this subject in other books and on the internet I found that its not that greate as it seem from the first impartation. Last point is that its coding style seem very poor (At least to me). It reminded me the way I used to write when I first started to program (using pSomething to point that the variable is a pointer..) He should have uses style similer to what is commenly used in other books (like the C++ programming laguage book). If you into design then Multi-Paradigm Design for C++ is better, and if you into template then "C++ templates" is much better.
Was this review helpful to you?
4.0 out of 5 stars template pathology April 7 2004
By A Customer
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.
Read more ›
Was this review helpful to you?
Want to see more reviews on this item?
Most recent customer reviews
4.0 out of 5 stars Christmas gift for my son
I bought this for my son for Christmas at his request. He is a computer engineer and seemed to like it.
Published 6 months ago by cj
4.0 out of 5 stars elegant solutions
Even so, it is relatively an old book, I learned a lot from it. I can see many ideas of this book are used in different C++ libraries, such as boost. Read more
Published on Jan. 12 2010 by pez
4.0 out of 5 stars Great, but an unfortunate number of typos
This book is quite mind blowing - just when I thought that there was nothing new to learn about C++, I read this book and realized that I couldn't have been more wrong. Read more
Published on Dec 16 2009 by Martin Kess
4.0 out of 5 stars Templatized Design Patterns in C++
Like the original Design Patterns book that this book is designed to supplement this book is split into two sections. Read more
Published on March 22 2004 by Jack D. Herrington
5.0 out of 5 stars Inspiring
This was a good buy for me, but I needed time to realize this. At first some techniques present in the book seemed too obscure to me, even though I use template metaprogramming... Read more
Published on Jan. 3 2004 by Boris Burger
5.0 out of 5 stars Excellent
Being a C++ programmer of 10+ years, I thought I was very proficient. This book has thought me to rethink my thought processes when it comes to design and implementation (not just... Read more
Published on Nov. 5 2003 by Sean Dwyer
5.0 out of 5 stars Template to the extreme.
The book open up the many possibility of C++ template, if you can read this book, you can read BOOST. Read more
Published on June 22 2003 by "g_m_c"
5.0 out of 5 stars Template to the extreme.
The book open up the many possibility of C++ template, if you can read this book, you can read BOOST. Read more
Published on June 22 2003 by "g_m_c"
5.0 out of 5 stars A very good book on C++ template
I strongly recommend this book to all C/C++ developer, regardless you are working on Windows, UNIX, LINUX or RTOS. Read more
Published on June 17 2003 by "g_m_c"
5.0 out of 5 stars Great book, probably the best one on templates
I re-read this book despite not being too impressed with it the first time and during my second reading a new pathway was opened into my understanding of templates. Read more
Published on May 15 2003
Search Customer Reviews
Only search this product's reviews

Look for similar items by category