Book Image

Advanced C++ Programming Cookbook

By : Dr. Rian Quinn
Book Image

Advanced C++ Programming Cookbook

By: Dr. Rian Quinn

Overview of this book

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.
Table of Contents (15 chapters)

What this book covers

Chapter 1, Getting Started with Library Development, teaches you how to develop your own libraries, including an explanation of the principle of least surprise, how to namespace everything, how to write header-only libraries, and how to ensure others will continue to use your libraries.

Chapter 2, Using Exceptions for Error Handling, covers more advanced topics of C++ exception and error handling, including a detailed explanation of the noexcept specifier and operator, how RAII supports resource management in the presence of exceptions, why throwing from a destructor should be avoided, and how to write your own exceptions.

Chapter 3, Implementing Move Semantics, provides a detailed explanation of C++ move semantics, including an explanation of the Big Five, how to make your class movable, how to write move-only (and non-move) non-copy style classes, how to properly implement a move constructor, why const && makes no sense, and how to use reference qualification.

Chapter 4, Using Templates for Generic Programming, teaches you how to write template functions like an expert, including how to implement your own SFINAE, how to perform perfect forwarding, how to use constexpr-if statements, how to leverage tuples with parameter packs, how to loop over parameter packs at compile time, how to use type traits to implement different versions of the same function, how to use template<auto>, and how to leverage explicit type declarations in your own applications.

Chapter 5, Concurrency and Synchronization, teaches you how to use std::mutex (and friends), when to use atomic types, how to handle const classes with thread-safety using the mutable keyword, how to write a thread-safe class, how to write a thread-safe wrapper, as well as how to write asynchronous C++ including promises and futures.

Chapter 6, Optimizing Your Code for Performance, covers how to profile and benchmark your C++, how to disassemble your C++ to better understand how to optimize your code, how to locate and remove unneeded memory allocations, and why noexcept helps with optimizations.

Chapter 7, Debugging and Testing, walks you through how to use Catch2 to unit test C++, how to use Google's ASAN and UBSAN sanitizers to dynamically analyze your code for memory corruption and undefined behavior, as well as how to use NDEBUG.

Chapter 8, Creating and Implementing Your Own Container, teaches you how to write your own container wrapper by creating a std::vector that is always sorted.

Chapter 9, Exploring Type Erasure, teaches you everything you need to know about type erasure, including how to erase types through inheritance and using template, how to implement the type erasure pattern, and how to implement the delegate pattern.

Chapter 10, An In-Depth Look at Dynamic Allocation, teaches you advanced topics in dynamic memory allocation, including how to properly use std::unique_ptr and std::shared_ptr, how to handle circular references, how to type cast smart pointers, and how the heap works behind the scenes to provide your application with dynamic memory.

Chapter 11, Common Patterns in C++, explains how different patterns in computer science are implemented in C++, including the factory pattern, the singleton pattern, the decorator pattern, and the observer pattern, as well as how to implement static polymorphism to write your own static interfaces without the need for virtual inheritance.

Chapter 12, A Closer Look at Type Deduction, provides a deep dive into how type deduction is performed in C++17, including how auto, decltype, and template deduce their types automatically. This chapter concludes with examples of how to write your own C++17 user-defined deduction guides.

Chapter 13, Bonus: Using C++20 Features, provides a sneak peek at the new features coming with C++20, including concepts, modules, ranges, and coroutines.