Book description
Become an expert at C++ by learning all the key C++ concepts and working through interesting exercises
Key Features
- Explore C++ concepts through descriptive graphics and interactive exercises
- Learn how to keep your development bug-free with testing and debugging
- Discover various techniques to optimize your code
Book Description
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.
What you will learn
- Delve into the anatomy and workflow of C++
- Study the pros and cons of different approaches to coding in C++
- Test, run, and debug your programs
- Link object files as a dynamic library
- Use templates, SFINAE, constexpr if expressions and variadic templates
- Apply best practice to resource management
Who this book is for
If you have worked in C++ but want to learn how to make the most of this language, especially for large projects, this book is for you. A general understanding of programming and knowledge of using an editor to produce code files in project directories is a must. Some experience with strongly typed languages, such as C and C++, is also recommended.
Table of contents
- Preface
- 1. Anatomy of Portable C++ Software
-
2A. No Ducks Allowed – Types and Deduction
- Introduction
- C++ Types
- Specifying Types – Variables
- Specifying Types – Functions
-
Creating User Types
- Enumerations
- Exercise 4: Enumerations – Old and New School
- Structures and Classes
- Fraction Class
- Constructors, Initialization, and Destructors
- Class Special Member Functions
- Implicit Versus Explicit Constructors
- Class Special Member Functions – Compiler Generation Rules
- Defaulting and Deleting Special Member Functions
- Rule of Three/Five and Rule of Zero
- Constructors – Initializing the Object
- Exercise 5: Declaring and Initializing Fractions
- Values Versus References and Const
- Exercise 6: Declaring and Using Reference Types
- Implementing Standard Operators
- Implementing the Output Stream Operator (<<)
- Structuring Our Code
- Exercise 7: Adding Operators to the Fraction Class
- Function Overloading
- Classes, Structs, and Unions
- Activity 1: Graphics Processing
- Summary
-
2B. No Ducks Allowed – Templates and Deduction
- Introduction
- Inheritance, Polymorphism, and Interfaces
- Visibility, Lifetime, and Access
- Templates – Generic Programming
- Type Aliases – typedef and using
-
Templates – More than Generic Programming
- Substitution Failure Is Not An Error – SFINAE
- Floating-Point Representations
- Constexpr if Expressions
- Non-Type Template Arguments
- Exercise 3: Implementing Stringify – specialization Versus constexpr
- Function Overloading Revisited
- Template Type Deduction
- Displaying the Deduced Types
- Template Type Deduction – the Details
- SFINAE Expression and Trailing Return Types
- Class Templates
- Summary
-
3. No Leaks Allowed - Exceptions and Resources
- Introduction
-
Exceptions in C++
- The Need for Exceptions
- Stack Unwinding
- Exercise 1: Implementing exceptions in Fraction and Stack
- What Happens When an Exception is Thrown?
- Throw-by-Value or Throw-by-Pointer
- Standard Library Exceptions
- Catching Exceptions
- Exercise 2: Implementing Exception Handlers
- CMake Generator Expressions
- Exception Usage Guidelines
-
Resource Management (in an Exceptional World)
- Resource Acquisition Is Initialization
- Exercise 3: Implementing RAII for Memory and File Handles
- Special Coding Techniques
- C++ doesn't Need finally
- RAII and the STL
- Who Owns This Object?
- Temporary Objects
- Move Semantics
- Implementing a Smart Pointer
- STL Smart Pointers
- std::unique_ptr
- std::shared_ptr
- std::weak_ptr
- Smart Pointers and Calling Functions
- Exercise 4: Implementing RAII with STL Smart Pointers
- Rule of Zero/Five – A Different Perspective
- Activity 1: Implementing Graphics Processing with RAII and Move
- When is a Function Called?
- Which Function to Call
- Identifiers
- Names
- Name lookup
- Argument-Dependent Lookup
- Caveat Emptor
- Exercise 5: Implementing Templates to Prevent ADL Issues
- Implicit Conversion
- Explicit – Preventing Implicit Conversion
- Contextual Conversion
- Exercise 6: Implicit and Explicit Conversions
- Activity 2: Implementing classes for Date Calculations
- Summary
- 4. Separation of Concerns - Software Architecture, Functions, and Variadic Templates
-
5. The Philosophers' Dinner – Threads and Concurrency
- Introduction
- Synchronous, Asynchronous, and Threaded Execution
-
Review Synchronization, Data Hazards, and Race Conditions
- Exercise 2: Writing an Example of Race Conditions
- Data Hazards
- RAW Dependency
- WAR Dependency
- WAW Dependency
- Resource Synchronization
- Event Synchronization
- Deadlock
- Move Semantics for Multithreading Closures
- Exercise 3: Moving Objects to a Thread Function
- Exercise 4: Creating and Working with an STL Container of Threads
- Futures, Promises, and Async
- Summary
-
6. Streams and I/O
-
Introduction
- Reviewing the I/O Portion of the Standard Library
- Predefined Standard Stream Objects
- Exercise 1: Overriding the Left Shift Operator, <<, for User-Defined Types
- File I/O Implementation Classes
- Exercise 2: Reading and Writing User-Defined Data Types to a File
- String I/O Implementation
- Exercise 3: Creating a Function for Replacement Words in a String
- I/O Manipulators
- I/O Manipulators for Changing the Numeric Base of the Stream
- Exercise 4: Displaying Entered Numbers in Different Numeric Bases
- I/O Manipulators for Floating-Point Formatting
- Exercise 5: Displaying Entered Floating-Point Numbers with Different Formatting
- I/O Manipulators for Boolean Formatting
- I/O Manipulators for Field Width and Fill Control
- I/O Manipulators for Other Numeric Formatting
- I/O Manipulators for Whitespace Processing
- Making Additional Streams
- How to Make an Additional Stream – Composition
- Exercise 6: Composing the Standard Stream Object in the User-Defined Class
- How to Make an Additional Stream – Inheritance
- Exercise 7: Inheriting the Standard Stream Object
- Leveraging Asynchronous I/O
- Asynchronous I/O on Windows Platforms
- Asynchronous I/O on Linux Platforms
- Exercise 8: Asynchronously Reading from a File in Linux
- Asynchronous Cross-Platform I/O Libraries
- Interaction of Threads and I/O
- Exercise 9: Developing a Thread-Safe Wrapper for std::cout
- Using Macros
- Activity 1: The Logging System for The Art Gallery Simulator
- Summary
-
Introduction
-
7. Everybody Falls, It's How You Get Back Up – Testing and Debugging
- Introduction
-
Unit Testing and Mock Testing
- Exercise 4: Creating Our First Unit Test Case
- Unit Testing Using Mock Objects
- Exercise 5: Creating Mock Objects
- Breakpoints, Watchpoints, and Data Visualization
- Working with the Stack Data Structure
- Activity 1: Checking the Accuracy of the Functions Using Test Cases and Understanding Test-Driven Development (TDD)
- Summary
-
8. Need for Speed – Performance and Optimization
- Introduction
- Performance Measurement
- Runtime Profiling
-
Optimization Strategies
- Compiler-Based Optimization
- Loop Unrolling
- Exercise 4: Using Loop Unrolling Optimizations
- Profile Guided Optimization
- Exercise 5: Using Profile Guided Optimization
- Parallelization
- Exercise 6: Using Compiler Parallelization
- Source Code Micro Optimizations
- Using the std::vector Container Efficiently
- Exercise 7: Optimizing Vector Growth
- Short-Circuit Logical Operators
- Exercise 8: Optimizing Logical Operators
- Branch Prediction
- Exercise 9: Optimization for Branch Prediction
- Further Optimizations
-
Cache Friendly Code
- Exercise 10: Exploring the Effect of Caches on Data Structures
- Exercise 11: Measuring the Impact of Memory Access
- Caching
- Prefetching
- Effects of Caching on Algorithms
- Optimizing for Cache-Friendliness
- Exercise 12: Exploring the Cost of Heap Allocations
- Struct of Arrays Pattern
- Exercise 13: Using the Struct of Arrays Pattern
- Algorithmic Optimizations
- Exercise 14: Optimizing a Word Count Program
- Activity 1: Optimizing a Spell Check Algorithm
- Summary
-
Appendix
- 1. Anatomy of Portable C++ Software
- 2A. No Ducks Allowed – Types and Deduction
- 2B. No Ducks Allowed – Templates and Deduction
- 3. The Distance between Can and Should – Objects, Pointers and Inheritance
- Activity 2: Implementing classes for Date Calculations
- 4. Separation of Concerns - Software Architecture, Functions, Variadic Templates
- 5. The Philosophers' Dinner – Threads and Concurrency
- 6. Streams and I/O
- 7. Everybody Falls, It's How You Get Back Up – Testing and Debugging
- 8. Need for Speed – Performance and Optimization
Product information
- Title: Advanced C++
- Author(s):
- Release date: October 2019
- Publisher(s): Packt Publishing
- ISBN: 9781838821135
You might also like
book
Expert C++
Design and architect real-world scalable C++ applications by exploring advanced techniques in low-level programming, object-oriented programming …
book
Mastering C++ Programming
Take your C++ coding to the next level by leveraging the latest features and advanced techniques …
book
Advanced C and C++ Compiling
Learning how to write C/C++ code is only the first step. To be a serious programmer, …
book
C++ High Performance
Write code that scales across CPU registers, multi-core, and machine clusters About This Book Explore concurrent …