Cover....1
Copyright....3
Contributors....4
Table of Contents....6
Preface....14
Part 1: Getting Started with C++ Features and Concepts....22
Chapter 1: An Introduction to Inheritance and Polymorphism....24
Classes and objects....24
Inheritance and class hierarchies....26
Polymorphism and virtual functions....32
Multiple inheritance....38
Summary....40
Questions....40
Further reading....41
Chapter 2: Class and Function Templates....42
Templates in C++....42
Function templates....43
Class templates....44
Variable templates....45
Non-type template parameters....45
Template template parameters....47
Template instantiations....49
Function templates....49
Class templates....52
Template specializations....55
Explicit specialization....55
Partial specialization....57
Template function overloading....60
Variadic templates....63
Lambda expressions....68
Concepts....73
Summary....78
Questions....78
Further reading....78
Chapter 3: Memory and Ownership....80
Technical requirements....80
What is memory ownership?....81
Well-designed memory ownership....81
Poorly designed memory ownership....82
Expressing memory ownership in C++....83
Expressing non-ownership....84
Expressing exclusive ownership....85
Expressing transfer of exclusive ownership....86
Expressing shared ownership....88
Owning objects and views....90
Resource-owning objects....90
Non-owning access to resource-owning objects....92
Summary....96
Questions....96
Further reading....97
Part 2: Common C++ Idioms....98
Chapter 4: Swap – from Simple to Subtle....100
Technical requirements....100
Swap and the standard template library....101
Swap and STL containers....101
Non-member swap....103
Swapping like the standard....104
When and why to use swap....104
Swap and exception safety....105
Other common swap idioms....107
How to implement and use swap correctly....109
Implementing swap....109
Using swap correctly....112
Summary....114
Questions....115
Chapter 5: A Comprehensive Look at RAII....116
Technical requirements....117
Resource management in C++....117
Installing the microbenchmark library....117
Installing Google Test....119
Counting resources....120
Dangers of manual resource management....122
Manual resource management is error-prone....122
Resource management and exception safety....125
The RAII idiom....127
RAII in a nutshell....127
RAII for other resources....132
Releasing early....133
Careful implementation of Resource Acquisition is Initialization objects....137
Downsides of RAII....139
Very modern RAII....141
Summary....147
Questions....147
Further reading....147
Chapter 6: Understanding Type Erasure....148
Technical requirements....148
What is type erasure?....148
Type erasure by example....149
From example to generalization....153
Type erasure as a design pattern....154
Type erasure as an implementation technique....155
How is type erasure implemented in C++?....156
Very old type erasure....157
Type erasure using inheritance....158
Type erasure without memory allocation....159
Type erasure without inheritance....160
Efficient type erasure....165
Performance of type erasure....169
Installing the micro-benchmark library....169
The overhead of type erasure....169
Guidelines for using type erasure....173
Summary....174
Questions....175
Chapter 7: SFINAE, Concepts, and Overload Resolution Management....176
Technical requirements....176
Overload resolution and overload sets....176
C++ function overloading....177
Template functions....180
Type substitution in template functions....182
Type deduction and substitution....183
Substitution failure....184
Substitution Failure Is Not An Error....186
When substitution failure is still an error....188
Where and why does substitution failure happen?....190
Concepts and constraints in C++20....193
Constraints in C++20....193
Concepts in C++20....196
Concepts and type restrictions....197
SFINAE techniques....198
SFINAE in C++20....198
SFINAE and type traits....200
Concepts before concepts....204
Constrained templates – the best practices....212
Summary....213
Questions....214
Part 3: C++ Design Patterns....216
Chapter 8: The Curiously Recurring Template Pattern....218
Technical requirements....218
What is wrong with a virtual function?....219
Introducing CRTP....221
CRTP and static polymorphism....226
Compile-time polymorphism....226
The compile-time pure virtual function....227
Destructors and polymorphic deletion....229
CRTP and access control....231
CRTP as a delegation pattern....232
Expanding the interface....233
CRTP and concepts....235
CRTP as an implementation technique....237
CRTP for code reuse....237
CRTP for generic interfaces....239
CRTP and policy-based design....242
Summary....245
Questions....245
Chapter 9: Named Arguments, Method Chaining, and the Builder Pattern....246
Technical requirements....246
What’s wrong with many arguments?....247
Aggregate parameters....251
Named arguments in C++....253
Method chaining....253
Method chaining and named arguments....254
Performance of the named arguments idiom....256
General method chaining....258
Method chaining versus method cascading....259
General method chaining....260
Method chaining in class hierarchies....260
The Builder pattern....263
Basics of the Builder pattern....264
The fluent Builder....265
The implicit builder....271
Summary....276
Questions....277
Chapter 10: Local Buffer Optimization....278
Technical requirements....278
The overhead of small memory allocations....279
The cost of memory allocations....279
Introducing local buffer optimization....282
The main idea....282
Effect of local buffer optimization....285
Additional optimizations....288
Local buffer optimization beyond strings....288
Small vector....289
Small queue....291
Type-erased and callable objects....293
Local buffer optimization in the standard library....296
Local buffer optimization in detail....297
Downsides of local buffer optimization....305
Summary....306
Questions....306
Chapter 11: ScopeGuard....308
Technical requirements....308
Error handling and resource acquisition is initialization....309
Error safety and exception safety....309
Resource Acquisition Is Initialization....312
The ScopeGuard pattern....316
ScopeGuard basics....316
Generic ScopeGuard....321
ScopeGuard and exceptions....326
What must not throw an exception....327
Exception-driven ScopeGuard....329
Type-erased ScopeGuard....333
Summary....336
Questions....337
Chapter 12: Friend Factory....338
Technical requirements....338
Friends in C++....338
How to grant friendship in C++....338
Friends versus member functions....339
Subtle details of friendship....343
Friends and templates....349
Friends of template classes....349
The template friend factory....352
Generating friends on demand....352
The friend factory and the Curiously Recurring Template Pattern....354
Summary....356
Questions....356
Chapter 13: Virtual Constructors and Factories....358
Technical requirements....358
Why constructors cannot be virtual....358
When does an object get its type?....359
The Factory pattern....363
The basics of the Factory method....363
Factory clarification....365
Arguments for factory methods....367
Dynamic type registry....371
Polymorphic factory....376
Factory-like patterns in C++....378
Polymorphic copy....378
CRTP Factory and return types....379
CRTP for Factory implementation....381
Factory and Builder....382
Summary....385
Questions....386
Chapter 14: The Template Method Pattern and the Non-Virtual Idiom....388
Technical requirements....388
The Template Method pattern....388
The Template Method in C++....389
Applications of the Template Method....390
Pre-and post-conditions and actions....392
The Non-Virtual Interface....395
Virtual functions and access....396
The NVI idiom in C++....397
Template Method vs Strategy....400
A note about destructors....401
Drawbacks of the Non-Virtual Interface....403
Composability....403
The Fragile Base Class problem....404
A cautionary note about template customization points....405
Summary....407
Questions....408
Part 4: Advanced C++ Design Patterns....410
Chapter 15: Policy-Based Design....412
Technical requirements....412
Strategy pattern and policy-based design....413
Foundations of policy-based design....413
Implementation of policies....421
Use of policy objects....424
Advanced policy-based design....431
Policies for constructors....431
Policies for test....438
Policy adapters and aliases....441
Using policies to control the public interface....443
Rebinding policies....451
Recommendations and guidelines....454
Strengths of the policy-based design....454
Disadvantages of policy-based design....455
Guidelines for policy-based designs....456
Summary....458
Questions....459
Chapter 16: Adapters and Decorators....460
Technical requirements....460
The decorator pattern....461
Basic decorator pattern....461
Decorators the C++ way....465
Polymorphic decorators and their limitations....470
Composable decorators....474
The Adapter pattern....476
Basic Adapter pattern....477
Function adapters....480
Adapter or Decorator....483
Compile-time adapters....485
Adapter versus policy....490
Adapter solution....491
Policy solution....500
Summary....508
Questions....508
Chapter 17: The Visitor Pattern and Multiple Dispatch....510
The Visitor pattern....511
What is the Visitor pattern?....511
Basic Visitor in C++....513
Visitor generalizations and limitations....517
Visiting complex objects....521
Visiting composite objects....521
Serialization and deserialization with Visitor....523
Acyclic Visitor....530
Visitors in modern C++....533
Generic Visitor....533
Lambda Visitor....536
Generic Acyclic Visitor....540
Compile-time Visitor....544
Visitor in C++17....551
Summary....556
Questions....557
Chapter 18: Patterns for Concurrency....558
Technical requirements....558
C++ and concurrency....559
Synchronization patterns....559
Mutex and locking patterns....560
No sharing is the best sharing....563
Waiting patterns....568
Lock-free synchronization patterns....572
Concurrent design patterns and guidelines....577
Thread safety guarantees....577
Transactional interface design....578
Data structures with access limitations....582
Concurrent execution patterns....585
Active object....586
Reactor Object pattern....590
Proactor Object pattern....591
Monitor pattern....592
Coroutine patterns in C++....595
Summary....599
Questions....599
Assessments....600
Index....614
Other Books You May Enjoy....623
C++ is a general-purpose programming language designed for efficiency, performance, and flexibility. Design patterns are commonly accepted solutions to well-recognized design problems. In essence, they are a library of reusable components, only for software architecture, and not for a concrete implementation.
This book helps you focus on the design patterns that naturally adapt to your needs, and on the patterns that uniquely benefit from the features of C++. Armed with the knowledge of these patterns, you'll spend less time searching for solutions to common problems and tackle challenges with the solutions developed from experience. You'll also explore that design patterns are a concise and efficient way to communicate, as patterns are a familiar and recognizable solution to a specific problem and can convey a considerable amount of information with a single line of code.
By the end of this book, you'll have a deep understanding of how to use design patterns to write maintainable, robust, and reusable software.
This book is for experienced C++ developers and programmers who wish to learn about software design patterns and principles and apply them to create robust, reusable, and easily maintainable programs and software systems.