I am pleased to announce that my latest book, Template Metaprogramming with C++ (ISBN 9781803243450), has been published by Packt and can be ordered from both Amazon and Packtpub.

About code. Mostly on C++
The Third Edition of my book, Modern C++ Programming Cookbook (ISBN 978-1835080542) has been published by Packt. The book can be ordered from Amazon and Packt. The book is organized in recipes, much like a cookbook. These recipes, in turn, are organized in sections that introduce you to the topic, list any necessary pre-requisites and … Read more
When it comes to format a piece of text in C++ there are several ways you can employ: I/O streams, particularly std::stringstream with stream operations (such as operator <<) printf family of functions, particularly sprintf the C++20 format library, particularly std::format / std::format_to any 3rd party library, {fmt} in particular (the source of the new … Read more
The C++20 standard added constraints and concepts to the language. This addition introduced two new keywords into the language, concept and requires. The former is used to declare a concept, while the latter is used to introduce a requires expression or a requires clause. These two could be confusion at first, so let’s take a look at which is which and what is their purpose.
One of the many things included in C++20 is the support for synchronizing output streams for operations that may have race conditions.
Logging is an important feature of any serious program. In general, one should use established libraries for logging. However, for the sake of showcasing several new features from C++20, now available in Visual Studio 2019 16.10, I will show, in this post, how one can quickly write a simple logging function.
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:
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?
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.