Initializing statement for if/switch/foreach

There are several statements in C++ whose syntax was modified in recent versions of the standard. I refer here to the if and switch statements that were modified in C++17 to include initializing statements, and the range-based for loop that supports the same as of C++20. Their general form is shown in the following table:

Express one of multiple options in a nice way

We often find ourselves writing if statements where a variable is compared with several values either to check if it matches one of them or that it doesn’t match any. Here is an example:

int option = ...;

// at lease a value is matched
if (option == 12 || option == 23 || option == 42)
{
   std::cout << "it's a good option\n";
}

// no value is matched
if (option != 12 && option != 23 && option != 42)
{
   std::cout << "it's a bad option\n";
}

This example has three comparison values for each case, but it could be 5, or 10, or any number. If it’s too many then perhaps a different approach should be taken. However, the question is, how do we express this in a simpler way in C++, rather than a long if condition?

The little functions that matter

Starting with C++20, some very useful functions for searching have been added to some standard containers, such as std::map, std::set, and std::string. These have been required for a long time and it’s good to see that the committee finally agreed upon their value. I hope this is the beginning of some wonderful additions.

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.

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…