Book description
Meta Description: With a wealth of practical recipes, this third edition isn't just a guide to C++23; it's a complete resource covering key features and techniques from C++11 through to C++23. It's designed to keep you ahead of the curve in C++.
Key Features
- Updated and packed with new recipes, including sync streams, std:expected and std:mdspan, and C++20/23 range adaptors
- Covers all significant features from all modern versions of the standard, providing comprehensive insights into modern C++
- Learn through a series of hands-on, self-contained recipes
- Purchase of the print or Kindle book includes a free eBook in PDF format
Book Description
The updated third edition of Modern C++ Programming Cookbook addresses the latest features of C++23, such as the stacktrace library, std::expected and std::mdspan classes, the header, formatting library improvements, and updates to the ranges library. It also gets into more C++20 topics not previously covered, such as sync streams and source_location.
The book is organized into practical recipes covering a wide range of real-world problems, helping you find the solutions you need quickly. You’ll find coverage of all the core concepts of modern C++ programming and features and techniques from C++11 through to C++23, meaning you’ll stay ahead of the curve by learning to incorporate the newest language and library improvements.
Beyond the core concepts and new features, you’ll explore recipes related to performance and best practices, how to implement useful patterns and idioms, like pimpl, named parameter, attorney-client, and the factory pattern, and how to complete unit testing with the widely used C++ libraries: Boost.Test, Google Test, and Catch2.
With the comprehensive coverage this C++ programming guide offers, by the end of the book you’ll have everything you need to build performant, scalable, and efficient applications in C++.
What you will learn
- Explore the new C++23 language and library features
- Go deep into the most useful C++20 features
- Learn to handle threading and concurrency for better performance
- Solve complex string manipulation tasks efficiently with regex
- Leverage the standard library for faster development
- Master the filesystem library to work with files and directories
- Work with different types of strings and understand compilation
- See how you can use CRTP, mixins and other patterns in C++
Who this book is for
This book is designed for entry- and intermediate-level programmers who already have a foundational understanding of the C++ programming language, but who are looking to master the language, implement the newest features, and become proficient modern C++ developers. Experienced C++ programmers can leverage the recipes in this book to quickly get up to speed on all the most important language and library features of C++11/14/17/20 and 23.
Table of contents
- Preface
-
Learning Modern Core Language Features
- Using auto whenever possible
- Creating type aliases and alias templates
- Understanding uniform initialization
- Understanding the various forms of non-static member initialization
- Controlling and querying object alignment
- Using scoped enumerations
- Using override and final for virtual methods
- Using range-based for loops to iterate on a range
- Enabling range-based for loops for custom types
- Using explicit constructors and conversion operators to avoid implicit conversion
- Using unnamed namespaces instead of static globals
- Using inline namespaces for symbol versioning
- Using structured bindings to handle multi-return values
- Simplifying code with class template argument deduction
- Using the subscript operator to access elements in a collection
-
Working with Numbers and Strings
- Understanding the various numeric types
- Limits and other properties of numeric types
- Converting between numeric and string types
- Understanding the various character and string types
- Printing Unicode characters to the output console
- Generating pseudo-random numbers
- Properly initializing a pseudo-random number generator
- Creating cooked user-defined literals
- Creating raw user-defined literals
- Using raw string literals to avoid escaping characters
- Creating a library of string helpers
- Verifying the format of a string using regular expressions
- Parsing the content of a string using regular expressions
- Replacing the content of a string using regular expressions
- Using std::string_view instead of constant string references
- Formatting and printing text with std::format and std::print
- Using std::format with user-defined types
-
Exploring Functions
- Defaulted and deleted functions
- Using lambdas with standard algorithms
- Using generic and template lambdas
- Writing a recursive lambda
- Writing function templates
- Writing a function template with a variable number of arguments
- Using fold expressions to simplify variadic function templates
- Implementing the higher-order functions map and fold
- Composing functions into a higher-order function
- Uniformly invoking anything callable
-
Preprocessing and Compilation
- Conditionally compiling your source code
- Using the indirection pattern for preprocessor stringification and concatenation
- Performing compile-time assertion checks with static_assert
- Conditionally compiling classes and functions with enable_if
- Selecting branches at compile time with constexpr if
- Providing metadata to the compiler with attributes
-
Standard Library Containers, Algorithms, and Iterators
- Using vector as a default container
- Using bitset for fixed-size sequences of bits
- Using vector<bool> for variable-size sequences of bits
- Using the bit manipulation utilities
- Finding elements in a range
- Sorting a range
- Initializing a range
- Using set operations on a range
- Using iterators to insert new elements into a container
- Writing your own random-access iterator
- Container access with non-member functions
- Selecting the right standard containers
-
General-Purpose Utilities
- Expressing time intervals with chrono::duration
- Working with calendars
- Converting times between time zones
- Measuring function execution time with a standard clock
- Generating hash values for custom types
- Using std::any to store any value
- Using std::optional to store optional values
- Chaining together computations that may or may not produce a value
- Using std::variant as a type-safe union
- Visiting a std::variant
- Using std::expected to return a value or an error
- Using std::span for contiguous sequences of objects
- Using std::mdspan for multi-dimensional views of sequences of objects
- Registering a function to be called when a program exits normally
- Using type traits to query properties of types
- Writing your own type traits
- Using std::conditional to choose between types
- Providing logging details with source_location
- Using the stacktrace library to print the call sequence
-
Working with Files and Streams
- Reading and writing raw data from/to binary files
- Reading and writing objects from/to binary files
- Using streams on fixed-size external buffers
- Using localized settings for streams
- Using I/O manipulators to control the output of a stream
- Using monetary I/O manipulators
- Using time I/O manipulators
- Working with filesystem paths
- Creating, copying, and deleting files and directories
- Removing content from a file
- Checking the properties of an existing file or directory
- Enumerating the content of a directory
- Finding a file
-
Leveraging Threading and Concurrency
- Working with threads
- Synchronizing access to shared data with mutexes and locks
- Finding alternatives for recursive mutexes
- Handling exceptions from thread functions
- Sending notifications between threads
- Using promises and futures to return values from threads
- Executing functions asynchronously
- Using atomic types
- Implementing parallel map and fold with threads
- Implementing parallel map and fold with tasks
- Implementing parallel map and fold with standard parallel algorithms
- Using joinable threads and cancellation mechanisms
- Synchronizing threads with latches, barriers, and semaphores
- Synchronizing writing to output streams from multiple threads
-
Robustness and Performance
- Using exceptions for error handling
- Using noexcept for functions that do not throw exceptions
- Ensuring constant correctness for a program
- Creating compile-time constant expressions
- Creating immediate functions
- Optimizing code in constant-evaluated contexts
- Using virtual function calls in constant expressions
- Performing correct type casts
- Implementing move semantics
- Using unique_ptr to uniquely own a memory resource
- Using shared_ptr to share a memory resource
- Consistent comparison with the operator <=>
- Comparing signed and unsigned integers safely
-
Implementing Patterns and Idioms
- Avoiding repetitive if-else statements in factory patterns
- Implementing the pimpl idiom
- Implementing the named parameter idiom
- Separating interfaces and implementations with the non-virtual interface idiom
- Handling friendship with the attorney-client idiom
- Static polymorphism with the curiously recurring template pattern
- Adding functionality to classes with mixins
- Handling unrelated types generically with the type erasure idiom
- Implementing a thread-safe singleton
-
Exploring Testing Frameworks
- Getting started with Boost.Test
- Writing and invoking tests with Boost.Test
- Asserting with Boost.Test
- Using fixtures in Boost.Test
- Controlling output with Boost.Test
- Getting started with Google Test
- Writing and invoking tests with Google Test
- Asserting with Google Test
- Using test fixtures with Google Test
- Controlling output with Google Test
- Getting started with Catch2
- Writing and invoking tests with Catch2
- Asserting with Catch2
- Controlling output with Catch2
-
C++ 20 Core Features
- Working with modules
- Understanding module partitions
- Specifying requirements on template arguments with concepts
- Using requires expressions and clauses
- Exploring abbreviated function templates
- Iterating over collections with the ranges library
- Exploring the standard range adaptors
- Converting a range to a container
- Creating your own range view
- Using constrained algorithms
- Creating a coroutine task type for asynchronous computations
- Creating a coroutine generator type for sequences of values
- Generating a sequence of values with the std::generator type
- Other Books You May Enjoy
- Index
Product information
- Title: Modern C++ Programming Cookbook - Third Edition
- Author(s):
- Release date: February 2024
- Publisher(s): Packt Publishing
- ISBN: 9781835080542
You might also like
book
Modern C++ Programming Cookbook - Second Edition
A pragmatic recipe book for acquiring a comprehensive understanding of the complexities and core fundamentals of …
book
Embedded Programming with Modern C++ Cookbook
Explore various constraints and challenges that embedded developers encounter in their daily tasks and learn how …
book
Discovering Modern C++, 2nd Edition
Write Powerful, Modern C++ Code for Scientific, Engineering, and Embedded Applications Discovering Modern C++, Second Edition, …
book
C++ Cookbook
Despite its highly adaptable and flexible nature, C++ is also one of the more complex programming …