0%

Book Description

A pragmatic recipe book for acquiring a comprehensive understanding of the complexities and core fundamentals of C++ programming

Key Features

  • Explore the latest language and library features of C++20 such as modules, coroutines, concepts, and ranges
  • Shed new light on the core concepts in C++ programming, including functions, algorithms, threading, and concurrency, through practical self-contained recipes
  • Leverage C++ features like smart pointers, move semantics, constexpr, and more for increased robustness and performance

Book Description

C++ has come a long way to be one of the most widely used general-purpose languages that is fast, efficient, and high-performance at its core.

The updated second edition of Modern C++ Programming Cookbook addresses the latest features of C++20, such as modules, concepts, coroutines, and the many additions to the standard library, including ranges and text formatting. The book is organized in the form of practical recipes covering a wide range of problems faced by modern developers.

The book also delves into the details of all the core concepts in modern C++ programming, such as functions and classes, iterators and algorithms, streams and the file system, threading and concurrency, smart pointers and move semantics, and many others. It goes into the performance aspects of programming in depth, teaching developers how to write fast and lean code with the help of best practices.

Furthermore, the book explores useful patterns and delves into the implementation of many idioms, including pimpl, named parameter, and attorney-client, teaching techniques such as avoiding repetition with the factory pattern. There is also a chapter dedicated to unit testing, where you are introduced to three of the most widely used libraries for C++: Boost.Test, Google Test, and Catch2.

By the end of the book, you will be able to effectively leverage the features and techniques of C++11/14/17/20 programming to enhance the performance, scalability, and efficiency of your applications.

What you will learn

  • Understand the new C++20 language and library features and the problems they solve
  • Become skilled at using the standard support for threading and concurrency for daily tasks
  • Leverage the standard library and work with containers, algorithms, and iterators
  • Solve text searching and replacement problems using regular expressions
  • Work with different types of strings and learn the various aspects of compilation
  • Take advantage of the file system library to work with files and directories
  • Implement various useful patterns and idioms
  • Explore the widely used testing frameworks for C++

Who this book is for

The book is designed for entry- or medium-level C++ programmers who have a basic knowledge of C++ and want to master the language and become prolific modern C++ developers. Experienced C++ programmers can leverage this book to strengthen their command of C++ and find a good reference to many language and library features of C++11/14/17/20.

Table of Contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Learning Modern Core Language Features
    1. Using auto whenever possible
      1. How to do it...
      2. How it works...
      3. See also
    2. Creating type aliases and alias templates
      1. How to do it...
      2. How it works...
      3. See also
    3. Understanding uniform initialization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Understanding the various forms of non-static member initialization
      1. How to do it...
      2. How it works...
      3. See also
    5. Controlling and querying object alignment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using scoped enumerations
      1. How to do it...
      2. How it works...
      3. See also
    7. Using override and final for virtual methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using range-based for loops to iterate on a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Enabling range-based for loops for custom types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Using explicit constructors and conversion operators to avoid implicit conversion
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Using unnamed namespaces instead of static globals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Using inline namespaces for symbol versioning
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Using structured bindings to handle multi-return values
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    14. Simplifying code with class template argument deduction
      1. How to do it...
      2. How it works...
      3. See also
  3. Working with Numbers and Strings
    1. Converting between numeric and string types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Limits and other properties of numeric types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Generating pseudo-random numbers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Initializing all bits of internal state of a pseudo-random number generator
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Creating cooked user-defined literals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating raw user-defined literals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using raw string literals to avoid escaping characters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating a library of string helpers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Verifying the format of a string using regular expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Parsing the content of a string using regular expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Replacing the content of a string using regular expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Using string_view instead of constant string references
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Formatting text with std::format
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Using std::format with user-defined types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  4. Exploring Functions
    1. Defaulted and deleted functions
      1. Getting started
      2. How to do it...
      3. How it works...
      4. See also
    2. Using lambdas with standard algorithms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Using generic and template lambdas
      1. Getting started
      2. How to do it...
      3. How it works...
      4. See also
    4. Writing a recursive lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Writing a function template with a variable number of arguments
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using fold expressions to simplify variadic function templates
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Implementing the higher-order functions map and fold
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Composing functions into a higher-order function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Uniformly invoking anything callable
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  5. Preprocessing and Compilation
    1. Conditionally compiling your source code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Using the indirection pattern for preprocessor stringification and concatenation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Performing compile-time assertion checks with static_assert
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Conditionally compiling classes and functions with enable_if
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Selecting branches at compile time with constexpr if
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Providing metadata to the compiler with attributes
      1. How to do it...
      2. How it works...
      3. See also
  6. Standard Library Containers, Algorithms, and Iterators
    1. Using vector as a default container
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Using bitset for fixed-size sequences of bits
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Using vector<bool> for variable-size sequences of bits
      1. Getting ready...
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using the bit manipulation utilities
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Finding elements in a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Sorting a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Initializing a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using set operations on a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Using iterators to insert new elements into a container
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Writing your own random-access iterator
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Container access with non-member functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  7. General-Purpose Utilities
    1. Expressing time intervals with chrono::duration
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    2. Working with calendars
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Converting times between time zones
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Measuring function execution time with a standard clock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Generating hash values for custom types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using std::any to store any value
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using std::optional to store optional values
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using std::variant as a type-safe union
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Visiting an std::variant
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Using std::span for contiguous sequences of objects
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    11. Registering a function to be called when a program exits normally
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Using type traits to query properties of types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    13. Writing your own type traits
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Using std::conditional to choose between types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  8. Working with Files and Streams
    1. Reading and writing raw data from/to binary files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    2. Reading and writing objects from/to binary files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Using localized settings for streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Using I/O manipulators to control the output of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Using monetary I/O manipulators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using time I/O manipulators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Working with filesystem paths
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating, copying, and deleting files and directories
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Removing content from a file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Checking the properties of an existing file or directory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Enumerating the content of a directory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Finding a file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  9. Leveraging Threading and Concurrency
    1. Working with threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Synchronizing access to shared data with mutexes and locks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Avoiding using recursive mutexes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Handling exceptions from thread functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Sending notifications between threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using promises and futures to return values from threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Executing functions asynchronously
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using atomic types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Implementing parallel map and fold with threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Implementing parallel map and fold with tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Implementing parallel map and fold with standard parallel algorithms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Using joinable threads and cancellation mechanisms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Using thread synchronization mechanisms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  10. Robustness and Performance
    1. Using exceptions for error handling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    2. Using noexcept for functions that do not throw exceptions
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    3. Ensuring constant correctness for a program
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Creating compile-time constant expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more…
      5. See also
    5. Creating immediate functions
      1. How to do it…
      2. How it works…
      3. See also
    6. Performing correct type casts
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Using unique_ptr to uniquely own a memory resource
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using shared_ptr to share a memory resource
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Implementing move semantics
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Consistent comparison with the operator <=>
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  11. Implementing Patterns and Idioms
    1. Avoiding repetitive if...else statements in factory patterns
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    2. Implementing the pimpl idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Implementing the named parameter idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Separating interfaces and implementations with the non-virtual interface idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Handling friendship with the attorney-client idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Static polymorphism with the curiously recurring template pattern
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Implementing a thread-safe singleton
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  12. Exploring Testing Frameworks
    1. Getting started with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    2. Writing and invoking tests with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Asserting with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Using fixtures in Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Controlling outputs with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Getting started with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Writing and invoking tests with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Asserting with Google Test
      1. How to do it...
      2. How it works...
      3. See also
    9. Using test fixtures with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Controlling output with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Getting started with Catch2
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Writing and invoking tests with Catch2
      1. How to do it...
      2. How it works...
      3. See also
    13. Asserting with Catch2
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Controlling output with Catch2
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. See also
  13. C++20 Core Features
    1. Working with modules
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    2. Understanding module partitions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Specifying requirements on template arguments with concepts
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using requires expressions and clauses
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Iterating over collections with the ranges library
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating your own range view
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a coroutine task type for asynchronous computations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a coroutine generator type for sequences of values
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  14. Bibliography
    1. Websites
    2. Articles and books
  15. Other Books You May Enjoy
  16. Index
18.225.209.95