SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
At long last, this much-awaited book delivers practical metaprogramming into the hands of the everyday C++ programmer.
° Presents the C++ Metaprogramming Library (MPL), a framework of powerful tools providing unprecedented new capability to the C++ power-user.
° Bjarne Stroustrup gave the equivalent of a blank check to the authors--meaning that he would accept anything they wrote into his Series, given their overwhelming expertise in this domain.
° CD-ROM contains the entire Boost Library.
Abrahams and Gurtovoy have written something close to a classic… marvelous fun to read…
Read the complete book review by Jack J. Woehr, Dr. Dobbs Journal, June 03, 2005
"If you're like me, you're excited by what people do with template metaprogramming (TMP) but are frustrated at the lack of clear guidance and powerful tools. Well, this is the book we've been waiting for. With help from the excellent Boost Metaprogramming Library, David and Aleksey take TMP from the laboratory to the workplace with readable prose and practical examples, showing that "compile-time STL" is as able as its runtime counterpart. Serving as a tutorial as well as a handbook for experts, this is the book on C++ template metaprogramming."
—Chuck Allison, Editor, The C++ Source
C++ Template Metaprogramming sheds light on the most powerful idioms of today's C++, at long last delivering practical metaprogramming tools and techniques into the hands of the everyday programmer.
A metaprogram is a program that generates or manipulates program code. Ever since generic programming was introduced to C++, programmers have discovered myriad "template tricks" for manipulating programs as they are compiled, effectively eliminating the barrier between program and metaprogram. While excitement among C++ experts about these capabilities has reached the community at large, their practical application remains out of reach for most programmers. This book explains what metaprogramming is and how it is best used. It provides the foundation you'll need to use the template metaprogramming effectively in your own work.
This book is aimed at any programmer who is comfortable with idioms of the Standard Template Library (STL). C++ power-users will gain a new insight into their existing work and a new fluency in the domain of metaprogramming. Intermediate-level programmers who have learned a few advanced template techniques will see where these tricks fit in the big picture and will gain the conceptual foundation to use them with discipline. Programmers who have caught the scent of metaprogramming, but for whom it is still mysterious, will finally gain a clear understanding of how, when, and why it works. All readers will leave with a new tool of unprecedented power at their disposal—the Boost Metaprogramming Library.
The companion CD-ROM contains all Boost C++ libraries, including the Boost Metaprogramming Library and its reference documentation, along with all of the book's sample code and extensive supplementary material.
Adding an Easy File Save and File Load Mechanism to Your C++ Program
Iterating Through Containers in C++, with Some Help from Boost's Lambda Library and Friends
An In-Depth Look at Metafunctions in C++
Download the Sample
Chapter related to this title.
Download the sample pages (includes Chapter 3 and Index)
Preface.
Acknowledgments.
Making the Most of This Book.
1. Introduction.
Getting Started.
So What's a Metaprogram?
Metaprogramming in the Host Language.
Metaprogramming in C++.
Why Metaprogramming?
When Metaprogramming?
Why a Metaprogramming Library?
2. Traits and Type Manipulation.
Type Associations.
Metafunctions.
Numerical Metafunctions.
Making Choices at Compile Time.
A Brief Tour of the Boost Type Traits Library.
Nullary Metafunctions.
Metafunction Definition.
History.
Details.
Exercises.
3. A Deeper Look at Metafunctions.
Dimensional Analysis.
Higher-Order Metafunctions.
Handling Placeholders.
More Lambda Capabilities.
Lambda Details.
Details.
Exercises.
4. Integral Type Wrappers and Operations.
Boolean Wrappers and Operations.
Integer Wrappers and Operations.
Exercises.
5. Sequences and Iterators.
Concepts.
Sequences and Algorithms.
Despite the existence of numerous real systems built with template metaprogramming and the MPL, many people still consider metaprogramming to be other-worldly magic, and often as something to be avoided in day-to-day production code. If you've never done any metaprogramming, it may not even have an obvious relationship to the work you do. With this book, we hope to lift the veil of mystery, so that you get an understanding not only of how metaprogramming is done, but also why and when. The best part is that while much of the mystery will have dissolved, we think you'll still find enough magic left in the subject to stay as inspired about it as we are.
Making the Most of This BookThe first few chapters of this book lay the conceptual foundation you'll need for most everything else we cover, and chapters generally build on material that has come before. That said, feel free to skip ahead for any reason—we've tried to make that possible by providing cross-references when we use terms introduced earlier on.
Chapter 10, Domain-Specific Embedded Languages, is an exception to the rule that later chapters depend on earlier ones. It focuses mostly on concepts, and only appears late in the book because at that point you'll have learned the tools and techniques to put Domain-Specific Embedded Languages into play in real code. If you only remember one chapter by the time you're done, make it that one.
Near the end of many chapters, you'll find a Details section that summarizes key ideas. These sections usually add new material that deepens the earlier discussion,* so even if you are inclined to skim them the first time through, we suggest you refer back to them later.
We conclude most chapters with exercises designed to help you develop both your programming and conceptual muscles. Those marked with asterisks are expected to be more of a workout than the others. Not all exercises involve writing code—some could be considered "essay questions"—and you don't have to complete them in order to move on to later chapters. We do suggest you look through them, give a little thought to how you'd answer each one, and try your hand at one or two; it's a great way to gain confidence with what you've just read.
Supplementary MaterialThis book comes with a companion CD that supplies the following items in electronic form
The index.html
file at the top level of the CD will provide you
with a convenient guide to all of its contents. Additional and updated material,
including the inevitable errata, will appear on the book's Web site: http://www.boost-consulting.com/mplbook.
You'll also find a place there to report any mistakes you might find.
To compile any of the examples, just put the CD's boost 1 32 0/
directory into your compiler's #include
path.
The libraries we present in this book go to great lengths to hide the problems of less-than-perfect compilers, so it's unlikely that you'll have trouble with the examples we present here. That said, we divide C++ compilers roughly into three categories.
A. Those with mostly conforming template
implementations. On these compilers, the examples and libraries "just work."
Almost anything released since 2001, and a few compilers released before then,
fall into this category.
B. Those that can be made to work, but require
some workarounds in user code.
C. Those that are too broken to use effectively
for template metaprogramming.
Appendix D lists the compilers that are known to fall into each of these categories. For those in category B, Appendix D refers to a list of portability idioms. These idioms have been applied to the copies of the book's examples that appear on the accompanying CD, but to avoid distracting the majority of readers they don't appear in the main text.
The CD also contains a portability table with a detailed report of how various compilers are doing with our examples. GCC is available free for most platforms, and recent versions have no problems handling the code we present here.
Even if you have a relatively modern compiler from category A, it might be a good idea to grab a copy of GCC with which to cross-check your code. Often the easiest way to decipher an inscrutable error message is to see what some other compiler has to say about your program. If you find yourself struggling with error messages as you try to do the exercises, you might want to skip ahead and read the first two sections of Chapter 8, which discusses how to read and manage diagnostics.
And now, on to C++ Template Metaprogramming!
— Dave and Aleksey
* We borrowed this idea from Andrew Koenig and Barbara Moo's Accelerated C++: Practical Programming By Example.
Download the Index
file related to this title.
Errata for this book can be found at:
http://www.boost-consulting.com/mplbook/errata.html