Modern C++ Programming Cookbook

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

Modern C++ Programming Cookbook

This ebook is included in a Mapt subscription
Marius Bancila

Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++
$10.00
$49.99
RRP $39.99
RRP $49.99
eBook
Print + eBook
Preview in Mapt

Book Details

ISBN 139781786465184
Paperback590 pages

Book Description

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.

Table of Contents

Chapter 1: Learning Modern Core Language Features
Introduction
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
Chapter 2: Working with Numbers and Strings
Introduction
Converting between numeric and string types
Limits and other properties of numeric types
Generating pseudo-random numbers
Initializing all bits of internal state of 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 string_view instead of constant string references
Chapter 3: Exploring Functions
Introduction
Defaulted and deleted functions
Using lambdas with standard algorithms
Using generic lambdas
Writing a recursive lambda
Writing a function template with a variable number of arguments
Using fold expressions to simplify variadic function templates
Implementing higher-order functions map and fold
Composing functions into a higher-order function
Uniformly invoking anything callable
Chapter 4: Preprocessor and Compilation
Introduction
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
Chapter 5: Standard Library Containers, Algorithms, and Iterators
Introduction
Using vector as a default container
Using bitset for fixed-size sequences of bits
Using vector<bool> for variable-size sequences of bits
Finding elements in a range
Sorting a range
Initializing a range
Using set operations on a range
Using iterators to insert new elements in a container
Writing your own random access iterator
Container access with non-member functions
Chapter 6: General Purpose Utilities
Introduction
Expressing time intervals with chrono::duration
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
Using std::variant as a type-safe union
Visiting a std::variant
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
Chapter 7: Working with Files and Streams
Introduction
Reading and writing raw data from/to binary files
Reading and writing objects from/to binary files
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
Chapter 8: Leveraging Threading and Concurrency
Introduction
Working with threads
Handling exceptions from thread functions
Synchronizing access to shared data with mutexes and locks
Avoiding using recursive mutexes
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
Chapter 9: Robustness and Performance
Introduction
Using exceptions for error handling
Using noexcept for functions that do not throw
Ensuring constant correctness for a program
Creating compile-time constant expressions
Performing correct type casts
Using unique_ptr to uniquely own a memory resource
Using shared_ptr to share a memory resource
Implementing move semantics
Chapter 10: Implementing Patterns and Idioms
Introduction
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
Implementing a thread-safe singleton
Chapter 11: Exploring Testing Frameworks
Introduction
Getting started with Boost.Test
Writing and invoking tests with Boost.Test
Asserting with Boost.Test
Using fixtures in Boost.Test
Controlling outputs with Boost.Test
Getting started with Google Test
Writing and invoking tests with Google Test
Asserting with Google Test
Using text fixtures with Google Test
Controlling output with Google Test
Getting started with Catch
Writing and invoking tests with Catch
Asserting with Catch
Controlling output with Catch

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

Authors

Table of Contents

Chapter 1: Learning Modern Core Language Features
Introduction
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
Chapter 2: Working with Numbers and Strings
Introduction
Converting between numeric and string types
Limits and other properties of numeric types
Generating pseudo-random numbers
Initializing all bits of internal state of 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 string_view instead of constant string references
Chapter 3: Exploring Functions
Introduction
Defaulted and deleted functions
Using lambdas with standard algorithms
Using generic lambdas
Writing a recursive lambda
Writing a function template with a variable number of arguments
Using fold expressions to simplify variadic function templates
Implementing higher-order functions map and fold
Composing functions into a higher-order function
Uniformly invoking anything callable
Chapter 4: Preprocessor and Compilation
Introduction
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
Chapter 5: Standard Library Containers, Algorithms, and Iterators
Introduction
Using vector as a default container
Using bitset for fixed-size sequences of bits
Using vector<bool> for variable-size sequences of bits
Finding elements in a range
Sorting a range
Initializing a range
Using set operations on a range
Using iterators to insert new elements in a container
Writing your own random access iterator
Container access with non-member functions
Chapter 6: General Purpose Utilities
Introduction
Expressing time intervals with chrono::duration
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
Using std::variant as a type-safe union
Visiting a std::variant
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
Chapter 7: Working with Files and Streams
Introduction
Reading and writing raw data from/to binary files
Reading and writing objects from/to binary files
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
Chapter 8: Leveraging Threading and Concurrency
Introduction
Working with threads
Handling exceptions from thread functions
Synchronizing access to shared data with mutexes and locks
Avoiding using recursive mutexes
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
Chapter 9: Robustness and Performance
Introduction
Using exceptions for error handling
Using noexcept for functions that do not throw
Ensuring constant correctness for a program
Creating compile-time constant expressions
Performing correct type casts
Using unique_ptr to uniquely own a memory resource
Using shared_ptr to share a memory resource
Implementing move semantics
Chapter 10: Implementing Patterns and Idioms
Introduction
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
Implementing a thread-safe singleton
Chapter 11: Exploring Testing Frameworks
Introduction
Getting started with Boost.Test
Writing and invoking tests with Boost.Test
Asserting with Boost.Test
Using fixtures in Boost.Test
Controlling outputs with Boost.Test
Getting started with Google Test
Writing and invoking tests with Google Test
Asserting with Google Test
Using text fixtures with Google Test
Controlling output with Google Test
Getting started with Catch
Writing and invoking tests with Catch
Asserting with Catch
Controlling output with Catch

Book Details

ISBN 139781786465184
Paperback590 pages
Read More

Read More Reviews