C++20 books

The C++20 standard is complete and is supposed to be published later this year after the voting of the final draft takes place. However, there are books already with C++20 content. In this blog post I present a list of them. The C++ Standard Library, 3rd edition – Rainer Grimm Rainer is an author, consultant,…

No more plain old data

When working in C++, you often hear about POD types (which stands for Plain Old Data). PODs are useful for communicating with code written in other programming languages (such as C or .NET languages). They can also be copied using memcpy (which is important because this is a fast, low-level function that provides performance benefits), and have other characteristics that are key for some scenarios. However, the new C++20 standard has deprecated the concept of POD types in favor of two more refined categories, which are trivial and standard-layout types. In this post, I will discuss what these categories are and when to use instead of POD.

A C++20 coroutine example

One of the most important new features in the C++20 is coroutines. A coroutine is a function that has the ability to be suspended and resumed. A function becomes a coroutine if it uses any of the following:

  • the co_await operator to suspend execution until resumed
  • the co_return keyword to complete execution and optionally return a value
  • the co_yield keyword to suspend execution and return a value

A coroutine must also have a return type that satisfies some requirements. However, the C++20 standard, only defines a framework for the execution of coroutines, but does not define any coroutine types satisfying such requirements. That means, we need to either write our own or rely on 3rd party libraries for this. In this post, I’ll show how to write some simple examples using the cppcoro library.

A custom C++20 range view

Some time ago, I wrote a short post about the C++20 ranges library with examples of how it can simplify our code. Let me take a brief example. Give a sequence of numbers, print the last two even numbers, but in reverse order.

Modules in Clang 11

In my previous post, I wrote about the support for C++20 modules in Visual Studio 2019 16.5. VC++ is not the only major compiler that has experimental support for modules. Clang has its own implementation, although only partial. In this post, I will discuss the support available in Clang 11. You can check the current status here.

Modules in VC++ 2019 16.5

Modules are one of the bigest changes in C++20 but the compilers’ support for them is a work in progress. The Visual C++ compiler has experimental support for modules that can be enabled by using the /experimental:module and /std:c++latest switches. In this post, I will walk through the core of the functionality available in Visual…

C++20 atomic_ref

C++11 provides the atomic operations library that features classes and functions that enable us to perform atomic operations using lock-free mechanisms. There are primarily two class templates in this library, std::atomic and std::atomic_flag. The latter, which defines an atomic boolean type, is guaranteed to always be lock-free and is implemented using the lock-free atomic CPU instructions. The former however, may actually be implemented using mutexes or other locking operations. In this article, we will look at a new class template, introduced in C++20, std::atomic_ref.

C++20 designated initializers

The C++20 standard provides new ways to initialize aggregates. These are:

  • list initialization with designated initializers, that has the following forms:
    T object = { .designator = arg1 , .designator { arg2 } ... };
    T object { .designator = arg1 , .designator { arg2 } ... };
  • direct initialization, that has the following form:
    T object (arg1, arg2, ...);

In this article, we will see how list initialization with designated initializers work.

Concepts versus SFINAE-based constraints

In some situations, we need to makes sure function templates can only be invoked with some specific types. SFINAE (that stands for Substitution Failure Is Not An Error) is a set of rules that specify how compilers can discard specializations from the overload resolution without causing errors. A way to achieve this is with the help of std::enable_if.

C++20 Concepts in Visual Studio 2019 16.3 Preview 2

Back in mid-August, Microsoft released the 2nd preview of Visual Studio 2019 16.3. This is the first version of Visual Studio to support concepts from C++20 both in the compiler and the standard library (header <concepts>) without the changes made at the ISO C++ standards meeting in Cologne. These changes are available when you compile with the /std:c++latest switch.

Concepts allow performing compile-time validation of template arguments and function dispatch based on properties of types. Concepts are very useful in libraries where they can be used to impose compile-time checks on the template arguments of functions or types. For instance, a generic algorithm for sorting a container would require the container type to be sortable for the program to even compile.

In this article, I will show an example with a concept that verifies that a type T can be converted to a std::string via a to_string() function, that is either a member of the class or a free function.