Advanced C++ Programming Cookbook: Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

A recipe-based guide to refining your C++ programming skills with the help of coding best practices, advanced programming concepts, and the latest features of C++17 and C++20

Key Features

  • Learn how to develop and design your own libraries
  • Find solutions to your app development problems and implement them in a highly reusable manner, following library development best practices
  • Explore advanced C++ features such as containers, coroutines, and modules

Book Description

If you think you've mastered C++ and know everything it takes to write robust applications, you'll be in for a surprise. With this book, you'll gain comprehensive insights into C++, covering exclusive tips and interesting techniques to enhance your app development process.

You'll kick off with the basic principles of library design and development, which will help you understand how to write reusable and maintainable code. You'll then discover the importance of exception safety, and how you can avoid unexpected errors or bugs in your code. The book will take you through the modern elements of C++, such as move semantics, type deductions, and coroutines. As you advance, you'll delve into template programming - the standard tool for most library developers looking to achieve high code reusability. You'll explore the STL and learn how to avoid common pitfalls while implementing templates. Later, you'll learn about the problems of multithreaded programming such as data races, deadlocks, and thread starvation. You'll also learn high-performance programming by using benchmarking tools and libraries. Finally, you'll discover advanced techniques for debugging and testing to ensure code reliability.

By the end of this book, you'll have become an expert at C++ programming and will have gained the skills to solve complex development problems with ease.

What you will learn

  • Solve common C++ development problems by implementing solutions in a more generic and reusable way
  • Achieve different levels of exception safety guarantees by introducing precise declarations
  • Write library-quality code that meets professional standards
  • Practice writing reliable, performant code that exposes consistent behavior in programs
  • Understand why you need to implement design patterns and how it's done
  • Work with complex examples to understand various aspects of good library design

Who this book is for

This book is for intermediate and expert-level C++ developers who are looking to explore the lesser known functionalities of the language to improve the efficiency of their code and the way they develop applications. Basic knowledge of object-oriented programming concepts and the Standard Template Library (STL) is assumed.

Table of Contents

  1. Getting Started With Library Development
  2. Using Exceptions for Error Handling
  3. Implementing Move Semantics
  4. Using Templates for Generic Programming
  5. Concurrency, Synchronisation, and Parallelization
  6. Optimizing Your Code for Performance
  7. Debugging and Testing
  8. Creating and Implementing Your own Container
  9. Exploring Type Erasure
  10. An In-Depth Look at Smart Pointers
  11. Common Patterns in C++
  12. A closer look at Type Deduction
  13. Bonus: Using C++20 Features

Author(s): Dr. Rian Quinn
Edition: 1
Publisher: Packt Publishing
Year: 2020

Language: English
Pages: 454
City: Birmingham
Tags: C++; C++20; C++ Programming Language; Templates; Concurrency; Type Deduction; Exception Handling; Move Semantics; Generic Programming; Object-Oriented Programming; Debugging

Cover
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Table of Contents
Preface
Chapter 1: Getting Started with Library Development
Technical requirements
Understanding the principle of least surprise
Getting ready
How to do it...
How it works...
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
Example 8
Example 9
How to namespace everything
Getting ready
How to do it...
How it works...
Example 1
Example 2
Header-only libraries
Getting ready
How to do it...
How it works...
How to handle includes
Global variables
Issues with C-style macros
How to implement a large library as header-only
Learning library development best practices
Getting ready
How to do it...
How it works... 
What about warnings?
Static and dynamic analysis
Documentation
CII Best Practices
Learning how to use the boost APIs
Getting ready
How to do it...
How it works... 
Example 1
Example 2
See also
Chapter 2: Using Exceptions for Error Handling
Technical requirements
Using the noexcept specifier
Getting ready
How to do it...
How it works...
Using the noexcept operator
Getting ready
How to do it...
How it works...
Using RAII
Getting ready
How to do it...
How it works...
Learning why to never throw exceptions in destructors
Getting ready
How to do it...
How it works...
Easily creating your own exception classes
Getting ready
How to do it...
How it works...
Chapter 3: Implementing Move Semantics
Technical requirements
Using compiler-generated special class member functions and the Big Five
Getting ready
How to do it...
How it works...
Making your class movable
Getting ready
How to do it...
How it works...
Move-only types
Getting ready
How to do it...
How it works...
Implementing the noexcept move constructor
Getting ready
How to do it...
How it works...
Learning to be wary of const&&
Getting ready
How to do it...
How it works...
Referencing qualified member functions
Getting ready
How to do it...
How it works...
Exploring objects that cannot be moved or copied
Getting ready
How to do it...
How it works...
Chapter 4: Using Templates for Generic Programming
Technical requirements
Implementing SFINAE
Getting ready
How to do it...
How it works...
Learning perfect forwarding
Getting ready
How to do it...
How it works...
Using if constexpr
Getting ready
How to do it...
How it works...
Using tuples to work with parameter packs
Getting ready
How to do it...
How it works...
Using type traits to overload functions and objects
Getting ready
How to do it...
How it works...
Learning how to implement template
Getting ready
How to do it...
How it works...
Working with explicit template declarations
Getting ready
How to do it...
How it works...
Chapter 5: Concurrency and Synchronization
Technical requirements
Working with mutexes
Getting ready
How to do it...
How it works...
std::mutex
std::lock_guard
std::recursive_mutex
std::shared_mutex
std::timed_mutex
Using atomic data types
Getting ready
How to do it...
How it works...
Understanding what const & mutable mean in the context of multiple threads
Getting ready
How to do it...
How it works...
Making a class thread-safe
Getting ready
How to do it...
How it works...
Synchronization wrappers and how to implement them
Getting ready
How to do it...
How it works...
Blocking operations versus asynchronous programming
Getting ready
How to do it...
How it works...
Working with promises and futures
Getting ready
How to do it...
How it works...
Chapter 6: Optimizing Your Code for Performance
Technical requirements
Benchmarking your code
Getting ready
How to do it...
How it works...
Looking at assembly code
Getting ready
How to do it...
How it works...
Reducing the number of memory allocations
Getting ready
How to do it...
How it works...
Declaring noexcept
Getting ready
How to do it...
How it works...
Chapter 7: Debugging and Testing
Technical requirements
Getting to grips with unit testing
Getting ready
How to do it...
How it works...
Working with ASAN, the address sanitizer
Getting ready
How to do it...
How it works...
Memory leak error
Memory deleted twice
Accessing invalid memory 
Using memory after deleting it
Deleting memory that was never allocated
Working with UBSAN, the undefined behavior sanitizer
Getting ready
How to do it...
How it works...
Divide-by-zero errors
Null-pointer dereferences
Out-of-bounds errors
Overflow errors
Using #ifndef NDEBUG to conditionally execute additional checks
Getting ready
How to do it...
How it works...
Chapter 8: Creating and Implementing Your Own Container
Technical requirements
Using a simple wrapper around std::vector
Getting ready
How to do it...
How it works...
Default constructor
Custom allocator constructor
Count constructors
Copy/move constructors
Initializer list constructor
Usage
Adding elements to our container
Usage of push/emplace
Adding the relevant parts of the std::set API
Getting ready
How to do it...
How it works...
Working with iterators
Getting ready
How to do it...
How it works...
Adding the relevant parts of the std::vector API
Getting ready
How to do it...
How it works...
Chapter 9: Exploring Type Erasure
Technical requirements
How to erase a type with inheritance
Getting ready
How to do it...
How it works...
Using C++ templates to write generic functions
Getting ready
How to do it...
How it works...
There's more...
See also
Learning the C++ type eraser pattern
Getting ready
How to do it...
How it works...
Implementing delegates with type erasing
Getting ready
How to do it...
How it works...
Adding a function signature to our delegate
Adding const support to our delegate
Adding support for one-to-many to our delegate
Adding support for non-member functions to our delegate
Chapter 10: An In-Depth Look at Dynamic Allocation
Technical requirements
Comparing std::shared_ptr and std::unique_ptr
Getting ready
How to do it...
How it works...
Converting from a std::unique_ptr into a std::shared_ptr
Getting ready
How to do it...
How it works...
Working with circular references
Getting ready
How to do it...
How it works...
Typecasting with smart pointers
Getting ready
How to do it...
How it works...
The heap under a microscope
 Getting ready
How to do it...
How it works...
Chapter 11: Common Patterns in C++
Technical requirements
Learning the factory pattern
Getting ready
How to do it...
How it works...
Using the singleton pattern properly
Getting ready
How to do it...
How it works...
Extending your objects with the decorator pattern
Getting ready
How to do it...
How it works...
Adding communication with the observer pattern
Getting ready
How to do it...
How it works...
Improving performance with static polymorphism
Getting ready
How to do it...
How it works...
Chapter 12: A Closer Look at Type Deduction
Technical requirements
Using auto and type deduction
Getting ready
How to do it...
How it works...
Learning how decltype type deduction rules work
Getting ready
How to do it...
How it works...
Working with template function type deduction
Getting ready
How to do it...
How it works...
Leveraging template class type deduction in C++17
Getting ready
How to do it...
How it works...
Working with user-defined type deduction in C++17
Getting ready
How to do it...
How it works...
Chapter 13: Bonus - Using C++20 Features
Technical requirements
Looking at Concepts in C++20
Getting ready
How to do it...
How it works...
Working with Modules in C++20
Getting ready
How to do it...
How it works...
Introducing std::span, a new view on arrays
Getting ready
How to do it...
How it works...
Working with Ranges in C++20
Getting ready
How to do it...
How it works...
Learning how to use Coroutines in C++20
Getting ready
How to do it...
How it works...
Other Books You May Enjoy
Index