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

Marius Bancila

Marius Bancila is a software engineer with 14 years of experience in developing solutions for the industrial and financial sectors. He focuses on Microsoft technologies and mainly develops desktop applications with C++ and C#. Over the years, he has worked with other languages and technologies including Java, HTML/CSS, PHP, and JavaScript.

Marius is passionate about sharing his technical expertise with others, and for that reason, he has been recognized as a Microsoft MVP for more than a decade. He has been an active contributor to forums and other developer communities where he has published many articles, for which he has won multiple awards. He also created and contributed to several open source libraries. He is a cofounder of Codexpert, a Romanian community for C++ developers. He is based in Timisoara, Romania, and works as a system architect, building accounting and logistic solutions for a major European software vendor.

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

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.