logo资料库

C++ Template Metaprogramming.pdf

第1页 / 共312页
第2页 / 共312页
第3页 / 共312页
第4页 / 共312页
第5页 / 共312页
第6页 / 共312页
第7页 / 共312页
第8页 / 共312页
资料共312页,剩余部分请下载后查看
1. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
2. Table of Contents
3. Copyright
4. The C++ In-Depth Series
4.1 Titles in the Series
5. Preface
6. Acknowledgments
6.1 Dave's Acknowledgments
6.2 Aleksey's Acknowledgments
7. Making the Most of This Book
7.1 Supplementary Material
7.2 Trying It Out
8. Chapter 1. Introduction
8.1 Section 1.1. Getting Started
8.2 Section 1.2. So What's a Metaprogram?
8.3 Section 1.3. Metaprogramming in the Host Language
8.4 Section 1.4. Metaprogramming in C++
8.5 Section 1.5. Why Metaprogramming?
8.6 Section 1.6. When Metaprogramming?
8.7 Section 1.7. Why a Metaprogramming Library?
9. Chapter 2. Traits and Type Manipulation
9.1 Section 2.1. Type Associations
9.2 Section 2.2. Metafunctions
9.3 Section 2.3. Numerical Metafunctions
9.4 Section 2.4. Making Choices at Compile Time
9.5 Section 2.5. A Brief Tour of the Boost Type Traits Library
9.6 Section 2.6. Nullary Metafunctions
9.7 Section 2.7. Metafunction Definition
9.8 Section 2.8. History
9.9 Section 2.9. Details
9.10 Section 2.10. Exercises
10. Chapter 3. A Deeper Look at Metafunctions
10.1 Section 3.1. Dimensional Analysis
10.2 Section 3.2. Higher-Order Metafunctions
10.3 Section 3.3. Handling Placeholders
10.4 Section 3.4. More Lambda Capabilities
10.5 Section 3.5. Lambda Details
10.6 Section 3.6. Details
10.7 Section 3.7. Exercises
11. Chapter 4. Integral Type Wrappers and Operations
11.1 Section 4.1. Boolean Wrappers and Operations
11.2 Section 4.2. Integer Wrappers and Operations
11.3 Section 4.3. Exercises
12. Chapter 5. Sequences and Iterators
12.1 Section 5.1. Concepts
12.2 Section 5.2. Sequences and Algorithms
12.3 Section 5.3. Iterators
12.4 Section 5.4. Iterator Concepts
12.5 Section 5.5. Sequence Concepts
12.6 Section 5.6. Sequence Equality
12.7 Section 5.7. Intrinsic Sequence Operations
12.8 Section 5.8. Sequence Classes
12.9 Section 5.9. Integral Sequence Wrappers
12.10 Section 5.10. Sequence Derivation
12.11 Section 5.11. Writing Your Own Sequence
12.12 Section 5.12. Details
12.13 Section 5.13. Exercises
13. Chapter 6. Algorithms
13.1 Section 6.1. Algorithms, Idioms, Reuse, and Abstraction
13.2 Section 6.2. Algorithms in the MPL
13.3 Section 6.3. Inserters
13.4 Section 6.4. Fundamental Sequence Algorithms
13.5 Section 6.5. Querying Algorithms
13.6 Section 6.6. Sequence Building Algorithms
13.7 Section 6.7. Writing Your Own Algorithms
13.8 Section 6.8. Details
13.9 Section 6.9. Exercises
14. Chapter 7. Views and Iterator Adaptors
14.1 Section 7.1. A Few Examples
14.2 Section 7.2. View Concept
14.3 Section 7.3. Iterator Adaptors
14.4 Section 7.4. Writing Your Own View
14.5 Section 7.5. History
14.6 Section 7.6. Exercises
15. Chapter 8. Diagnostics
15.1 Section 8.1. Debugging the Error Novel
15.2 Section 8.2. Using Tools for Diagnostic Analysis
15.3 Section 8.3. Intentional Diagnostic Generation
15.4 Section 8.4. History
15.5 Section 8.5. Details
15.6 Section 8.6. Exercises
16. Chapter 9. Crossing the Compile-Time/Runtime Boundary
16.1 Section 9.1. for_each
16.2 Section 9.2. Implementation Selection
16.3 Section 9.3. Object Generators
16.4 Section 9.4. Structure Selection
16.5 Section 9.5. Class Composition
16.6 Section 9.6. (Member) Function Pointers as Template Arguments
16.7 Section 9.7. Type Erasure
16.8 Section 9.8. The Curiously Recurring Template Pattern
16.9 Section 9.9. Explicitly Managing the Overload Set
16.10 Section 9.10. The "sizeof Trick"
16.11 Section 9.11. Summary
16.12 Section 9.12. Exercises
17. Chapter 10. Domain-Specific Embedded Languages
17.1 Section 10.1. A Little Language ...
17.2 Section 10.2. ... Goes a Long Way
17.3 Section 10.3. DSLs, Inside Out
17.4 Section 10.4. C++ as the Host Language
17.5 Section 10.5. Blitz++ and Expression Templates
17.6 Section 10.6. General-Purpose DSELs
17.7 Section 10.7. The Boost Spirit Library
17.8 Section 10.8. Summary
17.9 Section 10.9. Exercises
18. Chapter 11. A DSEL Design Walkthrough
18.1 Section 11.1. Finite State Machines
18.2 Section 11.2. Framework Design Goals
18.3 Section 11.3. Framework Interface Basics
18.4 Section 11.4. Choosing a DSL
18.5 Section 11.5. Implementation
18.6 Section 11.6. Analysis
18.7 Section 11.7. Language Directions
18.8 Section 11.8. Exercises
19. Appendix A. An Introduction to Preprocessor Metaprogramming
19.1 Section A.1. Motivation
19.2 Section A.2. Fundamental Abstractions of the Preprocessor
19.3 Section A.3. Preprocessor Library Structure
19.4 Section A.4. Preprocessor Library Abstractions
19.5 Section A.5. Exercise
20. Appendix B. The typename and template Keywords
20.1 Section B.1. The Issue
20.2 Section B.2. The Rules
21. Appendix C. Compile-Time Performance
21.1 Section C.1. The Computational Model
21.2 Section C.2. Managing Compilation Time
21.3 Section C.3. The Tests
22. Appendix D. MPL Portability Summary
23. CD-ROM Warranty
24. Bibliography
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond Table of Contents 1. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond ...................................................................................................................................................... 5 2. Table of Contents ...................................................................................................................... 6 3. Copyright ................................................................................................................................. 10 4. The C++ In-Depth Series .......................................................................................................... 11 4.1 Titles in the Series ............................................................................................................. 12 5. Preface .................................................................................................................................... 13 6. Acknowledgments ................................................................................................................... 13 6.1 Dave's Acknowledgments ................................................................................................. 13 6.2 Aleksey's Acknowledgments ............................................................................................. 14 7. Making the Most of This Book ................................................................................................ 14 7.1 Supplementary Material ................................................................................................... 15 7.2 Trying It Out ...................................................................................................................... 15 8. Chapter 1. Introduction.......................................................................................................... 16 8.1 Section 1.1. Getting Started ............................................................................................. 16 8.2 Section 1.2. So What's a Metaprogram? ......................................................................... 17 8.3 Section 1.3. Metaprogramming in the Host Language .................................................... 18 8.4 Section 1.4. Metaprogramming in C++ ............................................................................ 18 8.5 Section 1.5. Why Metaprogramming? ............................................................................. 21 8.6 Section 1.6. When Metaprogramming? ........................................................................... 22 8.7 Section 1.7. Why a Metaprogramming Library? .............................................................. 23 9. Chapter 2. Traits and Type Manipulation .............................................................................. 24 9.1 Section 2.1. Type Associations ......................................................................................... 24 9.2 Section 2.2. Metafunctions .............................................................................................. 27 9.3 Section 2.3. Numerical Metafunctions ............................................................................ 30 9.4 Section 2.4. Making Choices at Compile Time ................................................................. 31 9.5 Section 2.5. A Brief Tour of the Boost Type Traits Library ............................................... 35 9.6 Section 2.6. Nullary Metafunctions ................................................................................. 40 9.7 Section 2.7. Metafunction Definition ............................................................................... 41 9.8 Section 2.8. History .......................................................................................................... 41 9.9 Section 2.9. Details ........................................................................................................... 42 9.10 Section 2.10. Exercises ................................................................................................... 45 1
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 10. Chapter 3. A Deeper Look at Metafunctions ....................................................................... 48 10.1 Section 3.1. Dimensional Analysis .................................................................................. 48 10.2 Section 3.2. Higher-Order Metafunctions ...................................................................... 58 10.3 Section 3.3. Handling Placeholders ................................................................................ 60 10.4 Section 3.4. More Lambda Capabilities ......................................................................... 62 10.5 Section 3.5. Lambda Details ........................................................................................... 63 10.6 Section 3.6. Details ......................................................................................................... 67 10.7 Section 3.7. Exercises ..................................................................................................... 69 11. Chapter 4. Integral Type Wrappers and Operations ............................................................ 70 11.1 Section 4.1. Boolean Wrappers and Operations ............................................................ 70 11.2 Section 4.2. Integer Wrappers and Operations ............................................................. 77 11.3 Section 4.3. Exercises ..................................................................................................... 81 12. Chapter 5. Sequences and Iterators..................................................................................... 83 12.1 Section 5.1. Concepts ..................................................................................................... 83 12.2 Section 5.2. Sequences and Algorithms ......................................................................... 84 12.3 Section 5.3. Iterators ...................................................................................................... 84 12.4 Section 5.4. Iterator Concepts ....................................................................................... 85 12.5 Section 5.5. Sequence Concepts .................................................................................... 89 12.6 Section 5.6. Sequence Equality ...................................................................................... 94 12.7 Section 5.7. Intrinsic Sequence Operations ................................................................... 94 12.8 Section 5.8. Sequence Classes ....................................................................................... 95 12.9 Section 5.9. Integral Sequence Wrappers ...................................................................... 99 12.10 Section 5.10. Sequence Derivation ............................................................................ 100 12.11 Section 5.11. Writing Your Own Sequence ................................................................ 101 12.12 Section 5.12. Details ................................................................................................... 111 12.13 Section 5.13. Exercises ............................................................................................... 111 13. Chapter 6. Algorithms ........................................................................................................ 114 13.1 Section 6.1. Algorithms, Idioms, Reuse, and Abstraction ............................................ 114 13.2 Section 6.2. Algorithms in the MPL .............................................................................. 116 13.3 Section 6.3. Inserters ................................................................................................... 117 13.4 Section 6.4. Fundamental Sequence Algorithms ......................................................... 119 13.5 Section 6.5. Querying Algorithms ................................................................................ 122 13.6 Section 6.6. Sequence Building Algorithms ................................................................. 123 13.7 Section 6.7. Writing Your Own Algorithms .................................................................. 126 2
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 13.8 Section 6.8. Details ....................................................................................................... 126 13.9 Section 6.9. Exercises ................................................................................................... 127 14. Chapter 7. Views and Iterator Adaptors ............................................................................ 129 14.1 Section 7.1. A Few Examples ........................................................................................ 129 14.2 Section 7.2. View Concept ........................................................................................... 135 14.3 Section 7.3. Iterator Adaptors ...................................................................................... 135 14.4 Section 7.4. Writing Your Own View ............................................................................ 136 14.5 Section 7.5. History ...................................................................................................... 137 14.6 Section 7.6. Exercises ................................................................................................... 137 15. Chapter 8. Diagnostics ....................................................................................................... 139 15.1 Section 8.1. Debugging the Error Novel ....................................................................... 139 15.2 Section 8.2. Using Tools for Diagnostic Analysis .......................................................... 149 15.3 Section 8.3. Intentional Diagnostic Generation ........................................................... 152 15.4 Section 8.4. History ...................................................................................................... 164 15.5 Section 8.5. Details ....................................................................................................... 165 15.6 Section 8.6. Exercises ................................................................................................... 166 16. Chapter 9. Crossing the Compile-Time/Runtime Boundary .............................................. 166 16.1 Section 9.1. for_each ................................................................................................... 167 16.2 Section 9.2. Implementation Selection ........................................................................ 169 16.3 Section 9.3. Object Generators .................................................................................... 174 16.4 Section 9.4. Structure Selection ................................................................................... 175 16.5 Section 9.5. Class Composition .................................................................................... 179 16.6 Section 9.6. (Member) Function Pointers as Template Arguments............................. 182 16.7 Section 9.7. Type Erasure ............................................................................................. 184 16.8 Section 9.8. The Curiously Recurring Template Pattern .............................................. 190 16.9 Section 9.9. Explicitly Managing the Overload Set ...................................................... 195 16.10 Section 9.10. The "sizeof Trick".................................................................................. 197 16.11 Section 9.11. Summary .............................................................................................. 198 16.12 Section 9.12. Exercises ............................................................................................... 199 17. Chapter 10. Domain-Specific Embedded Languages ......................................................... 200 17.1 Section 10.1. A Little Language ... ................................................................................ 200 17.2 Section 10.2. ... Goes a Long Way ................................................................................ 202 17.3 Section 10.3. DSLs, Inside Out ...................................................................................... 210 17.4 Section 10.4. C++ as the Host Language ...................................................................... 212 3
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 17.5 Section 10.5. Blitz++ and Expression Templates .......................................................... 214 17.6 Section 10.6. General-Purpose DSELs .......................................................................... 220 17.7 Section 10.7. The Boost Spirit Library .......................................................................... 229 17.8 Section 10.8. Summary ................................................................................................ 235 17.9 Section 10.9. Exercises ................................................................................................. 236 18. Chapter 11. A DSEL Design Walkthrough ........................................................................... 236 18.1 Section 11.1. Finite State Machines ............................................................................. 236 18.2 Section 11.2. Framework Design Goals........................................................................ 239 18.3 Section 11.3. Framework Interface Basics ................................................................... 240 18.4 Section 11.4. Choosing a DSL ....................................................................................... 241 18.5 Section 11.5. Implementation...................................................................................... 247 18.6 Section 11.6. Analysis ................................................................................................... 252 18.7 Section 11.7. Language Directions ............................................................................... 254 18.8 Section 11.8. Exercises ................................................................................................. 254 19. Appendix A. An Introduction to Preprocessor Metaprogramming ................................... 256 19.1 Section A.1. Motivation ............................................................................................... 257 19.2 Section A.2. Fundamental Abstractions of the Preprocessor ...................................... 258 19.3 Section A.3. Preprocessor Library Structure ................................................................ 260 19.4 Section A.4. Preprocessor Library Abstractions ........................................................... 261 19.5 Section A.5. Exercise .................................................................................................... 278 20. Appendix B. The typename and template Keywords......................................................... 279 20.1 Section B.1. The Issue .................................................................................................. 279 20.2 Section B.2. The Rules .................................................................................................. 283 21. Appendix C. Compile-Time Performance ........................................................................... 290 21.1 Section C.1. The Computational Model ....................................................................... 291 21.2 Section C.2. Managing Compilation Time .................................................................... 293 21.3 Section C.3. The Tests .................................................................................................. 294 22. Appendix D. MPL Portability Summary .............................................................................. 306 23. CD-ROM Warranty .............................................................................................................. 307 24. Bibliography ........................................................................................................................ 308 4
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 1. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond By David Abrahams, Aleksey Gurtovoy Publisher : Addison Wesley Professional Pub Date : December 10, 2004 ISBN : 0-321-22725-5 Pages : 400 "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 disposalthe 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. 5
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 2. Table of Contents C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond By David Abrahams, Aleksey Gurtovoy Publisher : Addison Wesley Professional Pub Date : December 10, 2004 ISBN : 0-321-22725-5 Pages : 400 Copyright(See 3.) The C++ In-Depth Series(See 4.) Titles in the Series(See 4.1) Preface(See 5.) Acknowledgments(See 6.) Dave's Acknowledgments(See 6.1) Aleksey's Acknowledgments(See 6.2) Making the Most of This Book(See 7.) Supplementary Material(See 7.1) Trying It Out(See 7.2) Chapter 1. Introduction(See 8.) Section 1.1. Getting Started(See 8.1) Section 1.2. So What's a Metaprogram?(See 8.2) Section 1.3. Metaprogramming in the Host Language(See 8.3) Section 1.4. Metaprogramming in C++(See 8.4) Section 1.5. Why Metaprogramming?(See 8.5) Section 1.6. When Metaprogramming?(See 8.6) Section 1.7. Why a Metaprogramming Library?(See 8.7) Chapter 2. Traits and Type Manipulation(See 9.) Section 2.1. Type Associations(See 9.1) Section 2.2. Metafunctions(See 9.2) 6
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond Section 2.3. Numerical Metafunctions(See 9.3) Section 2.4. Making Choices at Compile Time(See 9.4) Section 2.5. A Brief Tour of the Boost Type Traits Library(See 9.5) Section 2.6. Nullary Metafunctions(See 9.6) Section 2.7. Metafunction Definition(See 9.7) Section 2.8. History(See 9.8) Section 2.9. Details(See 9.9) Section 2.10. Exercises(See 9.10) Chapter 3. A Deeper Look at Metafunctions(See 10.) Section 3.1. Dimensional Analysis(See 10.1) Section 3.2. Higher-Order Metafunctions(See 10.2) Section 3.3. Handling Placeholders(See 10.3) Section 3.4. More Lambda Capabilities(See 10.4) Section 3.5. Lambda Details(See 10.5) Section 3.6. Details(See 10.6) Section 3.7. Exercises(See 10.7) Chapter 4. Integral Type Wrappers and Operations(See 11.) Section 4.1. Boolean Wrappers and Operations(See 11.1) Section 4.2. Integer Wrappers and Operations(See 11.2) Section 4.3. Exercises(See 11.3) Chapter 5. Sequences and Iterators(See 12.) Section 5.1. Concepts(See 12.1) Section 5.2. Sequences and Algorithms(See 12.2) Section 5.3. Iterators(See 12.3) Section 5.4. Iterator Concepts(See 12.4) Section 5.5. Sequence Concepts(See 12.5) Section 5.6. Sequence Equality(See 12.6) Section 5.7. Intrinsic Sequence Operations(See 12.7) Section 5.8. Sequence Classes(See 12.8) Section 5.9. Integral Sequence Wrappers(See 12.9) Section 5.10. Sequence Derivation(See 12.10) 7
分享到:
收藏