Cover....1
FM....2
Copyright....3
Table of Contents....4
Preface....16
Chapter 1: Anatomy of Portable C++ Software....24
Introduction....25
Managing C++ Projects....26
The Code-Build-Test-Run Loop....26
Building a CMake Project....27
Exercise 1: Using CMake to Generate Ninja Build Files....28
Importing a CMake Project into Eclipse CDT....31
Exercise 2: Importing the CMake File into Eclipse CDT....32
Exercise 3: Adding New Source Files to CMake and Eclipse CDT....36
Activity 1: Adding a New Source-Header File Pair to the Project....39
Unit Testing....40
Preparing for the Unit Tests....40
Exercise 4: Preparing Our Project for Unit Testing....41
Building, Running, and Writing Unit Tests....42
Exercise 5: Building and Running Tests....43
Exercise 6: Testing the Functionality of Code....49
Activity 2: Adding a New Class and Its Test....53
Understanding Compilation, Linking, and Object File Contents....55
Compilation and Linking Steps....55
Exercise 7: Identifying Build Steps....55
The Linking Step....62
Diving Deeper: Viewing Object Files....63
Exercise 8: Exploring Compiled Code....64
Debugging C++ Code....69
Exercise 9: Debugging with Eclipse CDT....70
Writing Readable Code....79
Indentation and Formatting....79
Use Meaningful Names as Identifiers....81
Keeping Algorithms Clear and Simple....83
Exercise 10: Making Code Readable....85
Activity 3: Making Code More Readable....89
Summary....91
Chapter 2A: No Ducks Allowed – Types and Deduction....94
Introduction....95
C++ Types....96
C++ Fundamental Types....96
C++ Literals....96
Specifying Types – Variables....97
Exercise 1: Declaring Variables and Exploring Sizes....99
Specifying Types – Functions....109
Exercise 2: Declaring Functions ....111
Pointer Types....113
Exercise 3: Declaring and Using Pointers ....118
Creating User Types....122
Enumerations....122
Exercise 4: Enumerations – Old and New School....127
Structures and Classes....130
Fraction Class....131
Constructors, Initialization, and Destructors....132
Class Special Member Functions....134
Implicit Versus Explicit Constructors....135
Class Special Member Functions – Compiler Generation Rules....136
Defaulting and Deleting Special Member Functions....136
Rule of Three/Five and Rule of Zero....138
Constructors – Initializing the Object....138
Exercise 5: Declaring and Initializing Fractions....141
Values Versus References and Const....143
Exercise 6: Declaring and Using Reference Types....146
Implementing Standard Operators....147
Implementing the Output Stream Operator (<<)....148
Structuring Our Code....149
Exercise 7: Adding Operators to the Fraction Class....150
Function Overloading....154
Classes, Structs, and Unions....155
Activity 1: Graphics Processing....157
Summary....160
Chapter 2B: No Ducks Allowed – Templates and Deduction....162
Introduction....163
Inheritance, Polymorphism, and Interfaces....163
Inheritance and Access Specifiers....169
Abstract Classes and Interfaces....169
Exercise 1: Implementing Game Characters with Polymorphism....170
Classes, Structs, and Unions Revisited....174
Visibility, Lifetime, and Access....175
Namespaces....178
Templates – Generic Programming....179
What is Generic Programming?....180
Introducing C++ Templates....180
C++ Pre-Packaged Templates....182
Type Aliases – typedef and using....184
Exercise 2: Implementing Aliases....185
Templates – More than Generic Programming....186
Substitution Failure Is Not An Error – SFINAE....187
Floating-Point Representations....191
Constexpr if Expressions....192
Non-Type Template Arguments....193
Exercise 3: Implementing Stringify – specialization Versus constexpr....194
Function Overloading Revisited....197
Template Type Deduction....197
Displaying the Deduced Types....198
Template Type Deduction – the Details....199
SFINAE Expression and Trailing Return Types....206
Class Templates....209
Exercise 4: Writing a Class Template....209
Activity 1: Developing a Generic "contains" Template Function....213
Summary....214
Chapter 3: No Leaks Allowed - Exceptions and Resources....216
Introduction....217
Variable Scope and Lifetime....217
Exceptions in C++....223
The Need for Exceptions....223
Stack Unwinding....229
Exercise 1: Implementing exceptions in Fraction and Stack....229
What Happens When an Exception is Thrown?....235
Throw-by-Value or Throw-by-Pointer....235
Standard Library Exceptions....236
Catching Exceptions....237
Exercise 2: Implementing Exception Handlers....239
CMake Generator Expressions....241
Exception Usage Guidelines....242
Resource Management (in an Exceptional World) ....242
Resource Acquisition Is Initialization....243
Exercise 3: Implementing RAII for Memory and File Handles....246
Special Coding Techniques....249
C++ doesn't Need finally....250
RAII and the STL....250
Who Owns This Object?....250
Temporary Objects....252
Move Semantics....252
Implementing a Smart Pointer....253
STL Smart Pointers....258
std::unique_ptr ....259
std::shared_ptr ....260
std::weak_ptr....261
Smart Pointers and Calling Functions....264
Exercise 4: Implementing RAII with STL Smart Pointers....268
Rule of Zero/Five – A Different Perspective....273
Activity 1: Implementing Graphics Processing with RAII and Move....275
When is a Function Called?....276
Which Function to Call....277
Identifiers....278
Names....278
Name lookup....279
Argument-Dependent Lookup....279
Caveat Emptor ....281
Exercise 5: Implementing Templates to Prevent ADL Issues....283
Implicit Conversion....285
Explicit – Preventing Implicit Conversion ....285
Contextual Conversion....288
Exercise 6: Implicit and Explicit Conversions ....289
Activity 2: Implementing classes for Date Calculations....293
Summary....294
Chapter 4: Separation of Concerns - Software Architecture, Functions, and Variadic Templates....296
Introduction....297
The Pointer to Implementation (PIMPL) Idiom....297
Logical and Physical Dependencies....297
The Pointer to Implementation (PIMPL) Idiom....299
Advantages and Disadvantages of PIMPL....303
Implementing PIMPL with unique_ptr<>....303
unique_ptr<> PIMPL Special Functions....306
Exercise 1: Implementing a Kitchen with unique_ptr<>....309
Function Objects and Lambda Expressions....313
Function Pointers....313
What is a Function Object?....317
Exercise 2: Implementing function objects....320
std::function<> template....322
Exercise 3: Implementing callbacks with std::function....325
What is a Lambda Expression?....328
Capturing data into Lambdas....330
Exercise 4: Implementing Lambdas....332
Variadic Templates....336
Activity 1: Implement a multicast event handler ....340
Summary....341
Chapter 5: The Philosophers' Dinner – Threads and Concurrency....344
Introduction....345
Synchronous, Asynchronous, and Threaded Execution....346
Concurrency....346
Parallelism....347
Synchronous Execution....350
Asynchronous Execution....353
Exercise 1: Creating Threads in a Different Way....359
Review Synchronization, Data Hazards, and Race Conditions....363
Exercise 2: Writing an Example of Race Conditions....365
Data Hazards....369
RAW Dependency....369
WAR Dependency....370
WAW Dependency....371
Resource Synchronization....372
Event Synchronization....375
Deadlock....378
Move Semantics for Multithreading Closures....379
Exercise 3: Moving Objects to a Thread Function....382
Exercise 4: Creating and Working with an STL Container of Threads ....386
Futures, Promises, and Async....391
Exercise 5: Synchronization with Future Results....405
Activity 1: Creating a Simulator to Model the Work of the Art Gallery....408
Summary....412
Chapter 6: Streams and I/O....414
Introduction....415
Reviewing the I/O Portion of the Standard Library....416
Predefined Standard Stream Objects....418
Exercise 1: Overriding the Left Shift Operator, <<, for User-Defined Types....420
File I/O Implementation Classes....422
Exercise 2: Reading and Writing User-Defined Data Types to a File....426
String I/O Implementation....430
Exercise 3: Creating a Function for Replacement Words in a String....432
I/O Manipulators....435
I/O Manipulators for Changing the Numeric Base of the Stream ....435
Exercise 4: Displaying Entered Numbers in Different Numeric Bases....435
I/O Manipulators for Floating-Point Formatting....439
Exercise 5: Displaying Entered Floating-Point Numbers with Different Formatting....440
I/O Manipulators for Boolean Formatting....446
I/O Manipulators for Field Width and Fill Control....447
I/O Manipulators for Other Numeric Formatting....449
I/O Manipulators for Whitespace Processing....451
Making Additional Streams....453
How to Make an Additional Stream – Composition....454
Exercise 6: Composing the Standard Stream Object in the User-Defined Class....455
How to Make an Additional Stream – Inheritance....459
Exercise 7: Inheriting the Standard Stream Object....461
Leveraging Asynchronous I/O....465
Asynchronous I/O on Windows Platforms....465
Asynchronous I/O on Linux Platforms....475
Exercise 8: Asynchronously Reading from a File in Linux....477
Asynchronous Cross-Platform I/O Libraries....482
Interaction of Threads and I/O....485
Exercise 9: Developing a Thread-Safe Wrapper for std::cout....486
Using Macros....491
Activity 1: The Logging System for The Art Gallery Simulator....493
Summary....497
Chapter 7: Everybody Falls, It's How You Get Back Up – Testing and Debugging....500
Introduction....501
Assertions....502
Exercise 1: Writing and Testing Our First Assertion....502
Static Assertions....506
Exercise 2: Testing Static Assertions....507
Understanding Exception Handling....510
Exercise 3: Performing Exception Handling ....511
Unit Testing and Mock Testing....514
Exercise 4: Creating Our First Unit Test Case....515
Unit Testing Using Mock Objects....522
Exercise 5: Creating Mock Objects....523
Breakpoints, Watchpoints, and Data Visualization....532
Working with the Stack Data Structure....533
Activity 1: Checking the Accuracy of the Functions Using Test Cases and Understanding Test-Driven Development (TDD)....547
Summary....555
Chapter 8: Need for Speed – Performance and Optimization....558
Introduction....559
Performance Measurement....559
Manual Estimation....560
Studying Generated Assembly Code....561
Manual Execution Timing....570
Exercise 1: Timing a Program's Execution....571
Timing Programs without Side Effects....575
Source Code Instrumentation....576
Exercise 2: Writing a Code Timer Class....576
Runtime Profiling....582
Exercise 3: Using perf to Profile Programs....582
Optimization Strategies....584
Compiler-Based Optimization....584
Loop Unrolling....584
Exercise 4: Using Loop Unrolling Optimizations....585
Profile Guided Optimization....587
Exercise 5: Using Profile Guided Optimization....588
Parallelization....589
Exercise 6: Using Compiler Parallelization....589
Source Code Micro Optimizations....591
Using the std::vector Container Efficiently....591
Exercise 7: Optimizing Vector Growth....592
Short-Circuit Logical Operators....596
Exercise 8: Optimizing Logical Operators....596
Branch Prediction....599
Exercise 9: Optimization for Branch Prediction....599
Further Optimizations....602
Cache Friendly Code....603
Exercise 10: Exploring the Effect of Caches on Data Structures....603
Exercise 11: Measuring the Impact of Memory Access....607
Caching....611
Prefetching....611
Effects of Caching on Algorithms....612
Optimizing for Cache-Friendliness....613
Exercise 12: Exploring the Cost of Heap Allocations....614
Struct of Arrays Pattern....617
Exercise 13: Using the Struct of Arrays Pattern....617
Algorithmic Optimizations....621
Exercise 14: Optimizing a Word Count Program....622
Activity 1: Optimizing a Spell Check Algorithm....637
Summary....640
Appendix....642
Index....754
C++ is one of the most widely used programming languages and is applied in a variety of domains, right from gaming to graphical user interface (GUI) programming and even operating systems. If you're looking to expand your career opportunities, mastering the advanced features of C++ is key.
The book begins with advanced C++ concepts by helping you decipher the sophisticated C++ type system and understand how various stages of compilation convert source code to object code. You'll then learn how to recognize the tools that need to be used in order to control the flow of execution, capture data, and pass data around. By creating small models, you'll even discover how to use advanced lambdas and captures and express common API design patterns in C++. As you cover later chapters, you'll explore ways to optimize your code by learning about memory alignment, cache access, and the time a program takes to run. The concluding chapter will help you to maximize performance by understanding modern CPU branch prediction and how to make your code cache-friendly.
By the end of this book, you'll have developed programming skills that will set you apart from other C++ programmers.