Modern C++ Programming Cookbook

Book description

Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++

About This Book

  • Explore the most important language and library features of C++17, including containers, algorithms, regular expressions, threads, and more,
  • Get going with unit testing frameworks Boost.Test, Google Test and Catch,
  • Extend your C++ knowledge and take your development skills to new heights by making your applications fast, robust, and scalable.

Who This Book Is For

If you want to overcome difficult phases of development with C++ and leverage its features using modern programming practices, then this book is for you. The book is designed for both experienced C++ programmers as well as people with strong knowledge of OOP concepts.

What You Will Learn

  • Get to know about the new core language features and the problems they were intended to solve
  • Understand the standard support for threading and concurrency and know how to put them on work for daily basic tasks
  • Leverage C++'s features to get increased robustness and performance
  • Explore the widely-used testing frameworks for C++ and implement various useful patterns and idioms
  • Work with various types of strings and look at the various aspects of compilation
  • Explore functions and callable objects with a focus on modern features
  • Leverage the standard library and work with containers, algorithms, and iterators
  • Use regular expressions for find and replace string operations
  • Take advantage of the new filesystem library to work with files and directories
  • Use the new utility additions to the standard library to solve common problems developers encounter including string_view, any , optional and variant types

In Detail

C++ is one of the most widely used programming languages. Fast, efficient, and flexible, it is used to solve many problems. The latest versions of C++ have seen programmers change the way they code, giving up on the old-fashioned C-style programming and adopting modern C++ instead.

Beginning with the modern language features, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works. You will learn major concepts about the core programming language as well as common tasks faced while building a wide variety of software. You will learn about concepts such as concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes. These recipes will ensure you can make your applications robust and fast.

By the end of the book, you will understand the newer aspects of C++11/14/17 and will be able to overcome tasks that are time-consuming or would break your stride while developing.

Style and approach

This book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.

Table of contents

  1. Preface
    1. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
      6. Conventions
    2. Reader feedback
    3. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Learning Modern Core Language Features
    1. Introduction
    2. Using auto whenever possible
      1. How to do it...
      2. How it works...
      3. See also
    3. Creating type aliases and alias templates
      1. How to do it...
      2. How it works...
    4. Understanding uniform initialization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more
      5. See also
    5. Understanding the various forms of non-static member initialization
      1. How to do it...
      2. How it works...
    6. Controlling and querying object alignment
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using scoped enumerations
      1. How to do it...
      2. How it works...
    8. Using override and final for virtual methods
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. 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
    10. Enabling range-based for loops for custom types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. 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
    12. Using unnamed namespaces instead of static globals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Using inline namespaces for symbol versioning
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Using structured bindings to handle multi-return values
      1. Getting ready
      2. How to do it...
      3. How it works...
  3. Working with Numbers and Strings
    1. Introduction
    2. Converting between numeric and string types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Limits and other properties of numeric types
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Generating pseudo-random numbers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Initializing all bits of internal state of a pseudo-random number generator
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Creating cooked user-defined literals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Creating raw user-defined literals
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using raw string literals to avoid escaping characters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Creating a library of string helpers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. 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
    11. Parsing the content of a string using regular expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Replacing the content of a string using regular expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Using string_view instead of constant string references
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  4. Exploring Functions
    1. Introduction
    2. Defaulted and deleted functions
      1. Getting started
      2. How to do it...
      3. How it works...
    3. Using lambdas with standard algorithms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using generic lambdas
      1. Getting started
      2. How to do it...
      3. How it works...
      4. See also
    5. Writing a recursive lambda
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. 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
    7. 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
    8. Implementing 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
    9. 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
    10. Uniformly invoking anything callable
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  5. Preprocessor and Compilation
    1. Introduction
    2. Conditionally compiling your source code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Using the indirection pattern for preprocessor stringification and concatenation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Performing compile-time assertion checks with static_assert
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. 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
    6. Selecting branches at compile time with constexpr if
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Providing metadata to the compiler with attributes
      1. How to do it...
      2. How it works...
  6. Standard Library Containers, Algorithms, and Iterators
    1. Introduction
    2. Using vector as a default container
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. 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
    4. 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
    5. Finding elements in a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Sorting a range
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. 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 in 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. Introduction
    2. 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
    3. Measuring function execution time with a standard clock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Generating hash values for custom types
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using std::any to store any value
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using std::optional to store optional values
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. 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
    8. Visiting a std::variant
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. 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
    10. 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
    11. Writing your own type traits
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. 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. Introduction
    2. 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
    3. Reading and writing objects from/to binary files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Using localized settings for streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. 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
    6. Using monetary I/O manipulators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using time I/O manipulators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Working with filesystem paths
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Creating, copying, and deleting files and directories
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Removing content from a file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Checking the properties of an existing file or directory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. 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
    13. Finding a file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  9. Leveraging Threading and Concurrency
    1. Introduction
    2. Working with threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Handling exceptions from thread functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Synchronizing access to shared data with mutexes and locks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Avoiding using recursive mutexes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Sending notifications between threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. 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
    8. Executing functions asynchronously
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Using atomic types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Implementing parallel map and fold with threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. 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
  10. Robustness and Performance
    1. Introduction
    2. Using exceptions for error handling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Using noexcept for functions that do not throw
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Ensuring constant correctness for a program
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    5. Creating compile-time constant expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. 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
  11. Implementing Patterns and Idioms
    1. Introduction
    2. 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
    3. Implementing the pimpl idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Implementing the named parameter idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. 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
    6. Handling friendship with the attorney-client idiom
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more
      5. See also
    7. 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
    8. 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. Introduction
    2. Getting started with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Writing and invoking tests with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Asserting with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Using fixtures in Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Controlling outputs with Boost.Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Getting started with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Writing and invoking tests with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Asserting with Google Test
      1. How to do it...
      2. How it works...
      3. See also
    10. Using text fixtures with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Controlling output with Google Test
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Getting started with Catch
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    13. Writing and invoking tests with Catch
      1. How to do it...
      2. How it works...
      3. See also
    14. Asserting with Catch
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    15. Controlling output with Catch
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  13. Bibliography
    1. Articles and books

Product information

  • Title: Modern C++ Programming Cookbook
  • Author(s): Marius Bancila
  • Release date: May 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781786465184