Modern C++ Design
Modern C++ Design
Modern C++ Design
Generic Programming
and Design Patterns Applied
Andrei Alexandrescu
.~Addison-Wesley
Boston " San Francisco " New York " Toronto " Montreal
London " Munich " Paris " Madrid
Capetown " Sydney + Tokyo " Singapore " Mexico City
Contents
Part I Techniques
Chapter 2 Techniques 23
2.1 Compile-Time Assertions 23
2,2 Partial Template Specialization 26
2.3 Local Classes 28
2.4 Mapping Integral Constants to Types 29
2.5 Type-to-Type Mapping 31
2.6 Type Selection 33
2.7 Detecting Convertibility and Inheritance at Compile Time 34
2.8 A Wrapper Around type-i nfo 37
2.9 NuI lType and EmptyType 39
2 .10 Type Traits 40
2.11 Summary 46
Chapter 3 Typelists 49
3.1 The Need for Typelists 49
3.2 Defining Typelists 51
3.3 Linearizing Typelist Creation 52
3.4 Calculating Length 53
3.5 Intermezzo 54
3.6 Indexed Access 55
3.7 Searching Typelists 56
3.8 Appending to Typelists 57
3.9 Erasing a Type from a Typelist 58
3.10 Erasing Duplicates 59
3.11 Replacing an Element in a Typelist 60
3.12 Partially Ordering Typelists 61
3,13 Class Generation with Typelists
64
3.14 Summary
74
3.15 Typel i st Quick Facts
75
Chapter 4 Small-Obj ect Allocation
77
4.1 The Default Free Store Allocator
4.2 78
The Workings of a Memory Allocator
4.3 A Small-Object Allocator 78
4.4 Chunks
80
4.5 The Fixed-Size Allocator 81
4.6 The Smal 1 objAl 1 ocato r Class 84
4.7 A Hat Trick 87
4.8 Simple, Complicated, Yet Simple in the End 89
4.9 Admirtistrivia 92
4.10 Summary 93
4.11 Small-Object Allocator Quick Facts 94
94
Contents Vii
Part 11 Components 97
Chapter 10 Visitor
10.1 Visitor Basics 235
10.2 235
Overloading and the Catch-All Function
10.3 An Implementation Refinement : The Acyclic Visitor 242
10.4 A Generic Implementation of Visitor 243
10.5 Back to the "Cyclic" Visitor 248
10.6 Hooking Variations 255
10.7 Summary 258
260
10.8 Vi sitor Generic Component Quick Facts
261
Contents ix
Bibliography 317.