Revisiting C++
This chapter acts as a refresher on C++ 11-20, which will be used throughout this book. We'll explain why C++ represents a great opportunity that shouldn't be missed when it comes to writing good quality code that's concise and more portable than ever.
This chapter does not contain all the new features introduced by C++ (11 through 20) – just the ones we will be using for the rest of this book. Specifically, you'll get a refresher (if you already know) or learn (if you are new) about the most essential new C++ skills needed to write modern code. You'll work, hands-on, with lambda expressions, atomics, and move semantics, just to mention a few.
This chapter will cover the following recipes:
- Understanding C++ primitive types
- Lambda expressions
- Automatic type deduction and decltype
- Learning how atomic...
Technical requirements
To let you try out the programs in this chapter immediately, we've set up a Docker image that has all the tools and libraries we'll need throughout this book. It's based on Ubuntu 19.04.
In order to set it up, follow these steps:
- Download and install the Docker Engine from www.docker.com.
- Pull the image from Docker Hub: docker pull kasperondocker/system_programming_cookbook:latest.
- The image should now be available. Type in the following command to view the image: docker images.
- Now, you should have the following image: kasperondocker/system_programming_cookbook.
- Run the Docker image with an interactive shell with the help of the following command: docker run -it --cap-add sys_ptrace kasperondocker/system_programming_cookbook:latest /bin/bash.
- The shell on the running container is now available...
Understanding C++ primitive types
This recipe will show all the primitive data types defined by the C++ standard, as well as their size.
How to do it...
In this section, we'll have a closer look at what primitives the C++ standard defines and what other information is important. We'll also learn that although the standard does not define a size for each, it defines another important parameter:
- First, open a new Terminal and type in the following program:
#include <iostream>
#include <limits>
int main ()
{
// integral types section
std::cout << "char " << int(std::numeric_limits<char>::min())
<< "-" << int(std::numeric_limits<char...
Lambda expressions
A lambda expression (or lambda function) is a convenient way of defining an anonymous, small, and one-time use function to be used in the place right where it is needed. Lambda is particularly useful with Standard Template Library (STL), as we'll see.
How to do it...
In this section, we'll write some code in order to get familiar with lambda expressions. Although the mechanics are important, pay attention to the code readability with lambda, especially in conjunction with STL. Follow these steps:
- In this program, the lambda function gets an integer and prints it to standard output. Let's open a file named lambda_01.cpp and write the following code in it:
#include <iostream...
Automatic type deduction and decltype
C++ offers two mechanisms for deducting types from an expression: auto and decltype(). auto is used to deduce a type from its initializer, while decltype() is used to deduce a type for more complex cases. This recipe will show examples of how to use both.
How to do it...
It might be handy (and it actually is) to avoid explicitly specifying the type of variable that will be used, especially when it is particularly long and used very locally:
- Let's start with a typical example:
std::map<int, std::string> payslips;
// ...
for (std::map<int,
std::string>::const_iterator iter = payslips.begin();
iter !=payslips.end(); ++iter)
{
// ...
}
- Now, let&apos...
Learning how atomic works
Traditionally, C and C++ have a long tradition of portable code for system programming. The atomic feature that was introduced in the C++11 standard reinforces this by adding, natively, the guarantee that an operation is seen as atomic by other threads. Atomic is a template, such as template <class T> struct atomic; or template <class T> struct atomic<T*>;. C++20 has added shared_ptr and weak_ptr to T and T*. Any operation that's performed on the atomic variable is now protected from other threads.
How to do it...
std::atomic is an important aspect of modern C++ for dealing with concurrency. Let's write some code to master the...
Learning how nullptr works
Before C++11, the NULL identifier was meant to be used for pointers. In this recipe, we'll see why this was a problem and how C++11 solved it.
How to do it...
To understand why nullptr is important, let's look at the problem with NULL:
- Let's write the following code:
bool speedUp (int speed);
bool speedUp (char* speed);
int main()
{
bool ok = speedUp (NULL);
}
- Now, let's rewrite the preceding code using nullptr:
bool speedUp (int speed);
bool speedUp (char* speed);
int main()
{
bool ok = speedUp (nullptr);
}
How it works...
Smart pointers – unique_ptr and shared_ptr
This recipe will show the basic usage of unique_ptr and shared_ptr. These smart pointers are the main helpers for programmers who don't want to deal with memory deallocation manually. Once you've learned how to use them properly, this will save headaches and nights of debugging sessions.
How to do it...
In this section, we'll look at the basic use of two smart pointers, std::unique_ptr and std::shared_ptr:
- Let's develop a unique_ptr example by developing the following class:
#include <iostream>
#include <memory>
class CruiseControl
{
public:
CruiseControl()
{
std::cout << "CruiseControl object created" <...
Learning how move semantics works
We know copies are expensive, especially heavy objects. The move semantics that were introduced in C++11 help us avoid expensive copies. The foundational concept behind std::move and std::forward is the rvalue reference. This recipe will show you how to use std::move.
How to do it...
Let's develop three programs to learn about std::move and its universal reference:
- Let's start by developing a simple program:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> a = {1, 2, 3, 4, 5};
auto b = std::move(a);
std::cout << "a: " << a.size() << std::endl;
std::cout << "b: " << b.size() <...
Understanding concurrency
In the past, it was common for a C++ developer to write programs by using threading libraries or native threading mechanisms (for example pthread, a Windows thread). Since C++11, this has changed drastically and concurrency is another big feature that was added that goes in the direction of a self-consistent language. The two new features we'll look at in this recipe are std::thread and std::async.
How to do it...
In this section, we'll learn how to use std::thread with a basic scenario (create and join) and how to pass and receive parameters to it:
- std::thread: By using the basic thread methods, create and join, write the following code:
#include <iostream>
#include <...
Understanding the filesystem
C++17 marks another huge milestone in terms of new features. The filesystem library provides a simpler way of interacting with the filesystem. It was inspired by Boost.Filesystem (available since 2003). This recipe will show its basics features.
How to do it...
In this section, we'll show two examples of the filesystem library by using directory_iterator and create_directories. Although there is definitely more under this namespace, the goal of these two snippets is to highlight their simplicity:
- std::filesystem::directory_iterator: Let's write the following code:
#include <iostream>
#include <filesystem>
int main()
{
for(auto& p: std::filesystem::directory_iterator...
The C++ Core Guidelines
The C++ Core Guidelines are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many years of discussion and design across a number of organizations. Their design encourages general applicability and broad adoption but they can be freely copied and modified to meet your organization's needs. More precisely, these guidelines are referring to the C++14 standard.
Getting ready
Go over to GitHub and go to the C++ Core Guideline document (http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines), as well as to the GitHub project page: https://github.com/isocpp/CppCoreGuidelines.
Adding GSL in your makefile
"The GSL is the small set of types and aliases specified in these guidelines. At the time of writing, their specification herein is too sparse; we plan to add a WG21-style interface specification to ensure that different implementations agree, and to propose as a contribution for possible standardization, subject as usual to whatever the committee decides to accept/improve/alter/reject." – FAQ.50 of the C++ Core Guidelines.
Getting ready
Go to GitHub and go to the C++ Core Guideline document: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.
How to do it...
...Understanding concepts
A concept is a compile-time predicate that's used in conjunction with templates. The C++20 standard definitely boosted generic programming by providing more compile-time opportunity for the developer to communicate its intention. We can visualize concepts such as requirements (or constraints) the user of the template must adhere to. Why do we need concepts? Do you have do define concepts by yourself? This recipe will answer these and many more questions.
How to do it...
In this section, we will develop a concrete template example using concepts:
- We want to create our own version of the std::sort template function from the C++ standard library. Let's start by writing the...
Using span
We may come across cases where we need to write a method but we'd like to have the flexibility to accept a plain array or STL containers as input. std::span solves this problem. It gives the user a view into a contiguous sequence of elements. This recipe will teach you how to use it.
How to do it...
In this recipe, we'll write a method with one parameter (std::span) that can be used in different contexts. Then, we'll highlight the flexibility it offers:
- Let's start by adding the includes we need. Then, we need to define the print method by passing the container variable of the std::span type:
#include <iostream>
#include <vector>
#include <array>
#include <span>...
Learning how Ranges work
The C++20 standard added Ranges, which are an abstraction of containers that allow the program to operate uniformly on containers' elements. Furthermore, Ranges represent a very modern and concise way of writing expressive code. We'll learn that this expressiveness is even greater with pipes and adaptors.
How to do it...
In this section, we'll write a program that will help us learn the main use case of Ranges in conjunction with pipes and adaptors. Given an array of temperatures, we want to filter out the negative ones and convert the positives (warm temperatures) into Fahrenheit:
- On a new source file, type the following code. As you can see, two lambda functions and a for ...
Learning how modules work
Before C++20, there was only one way of structuring a program in parts: through the #include directive (which is resolved by the precompiler). The latest standard added another and more modern way of achieving the same result, called module. This recipe will show you how to write code using modules and the differences between #include and module.
How to do it...
In this section, we'll write a program composed of two modules. This program is an improvement of the one we developed in the Learning how Range works recipe. We'll encapsulate the temperature code in a module and use it in a client module. Let's get started:
- Let's create a new .cpp source file called temperature...