Slides of My Presentation at CppCon 2023: C++23 An Overview of Almost All New and Updated Features

This year at CppCon 2023, I gave the following session:

  • “C++23 An Overview of Almost All New and Updated Features”

You can find the slides of the session below.

Share

Next BeCPP UG Meeting Planned for October 31st, 2023

I’m happy to announce that the next meeting of the Belgian C++ Users Group is planned for Tuesday October 31st, 2023 at 18:00 at DAE and Howest (Howest, Campus Kortrijk Weide – The Penta).

DAE ( https://digitalartsandentertainment.be/ ) and Howest ( https://www.howest.be/ ) are sponsoring this event by providing the location, drinks and catering.

DAE Logo Howest Logo

The agenda is as follows:

  • 18:00: Reception with food.
  • 18:30: Session 1: Elevating Precision in C++: A Journey below the Surface of Floating-Point (Tom Tesch)
    Floating-point numbers, with their varying levels of precision, serve as foundational components in the toolkit of both new and seasoned C++ developers. In this presentation, we will uncover the inner workings of floating-point variables, explore their limitations, and discuss strategies for extending precision while being mindful of associated trade-offs. Join us as we embark on a journey into the realm of floating-point arithmetic in C++, offering insights that will enhance your programming finesse.
  • 19:30: Break
  • 19:45: Session 2: Coroutines: Don’t try this at home (Lieven de Cock)
    In C++20 we got the core language feature “coroutines”. Mostly oriented to the advanced c++ programmers, and not for the mere mortals, since it requires a lot of boilerplate one would not like to write. It provides the fundamentals to further build upon. Unfortunately the STL library did not build on it yet, more was to come in C++23.
    In C++23 we will only get one follow up: std::generator.
    So let’s revisit coroutines. What are they? A function that can be suspended and resumed. Easily said, but how to make use of it and write that boilerplate after all. We will look at the coroutine from 2 different angles: the user code and the compiler, and we will inspect what each wants or would like to have, and where we need to glue them together and what is needed for that purpose. We will give insights on topics like: coroutine frame, couroutine handle, promise_type, the interface or api.
    And if time permits we will look at an example of coroutines in use in asynchronous code (because a misconception is that coroutines (purely) have to do with threads and async’s).
  • 20:45: Introduction to DAE and Howest, followed by a drink.

The event is free for everyone, but you need to register for it.

There are 250 seats available for this event.

Note: The deadline for registrations is October 29th, 2023!

Share

Next BeCPP UG Meeting Planned for May 11th, 2023

I’m happy to announce that the next meeting of the Belgian C++ Users Group is planned for Thursday May 11th, 2023 at 18:00 at Twikit.

Twikit ( https://www.twikit.com/ ) is sponsoring this event by providing the location, drinks and catering.

Twikit Logo

The agenda is as follows:

  • 18:00: Reception with food.
  • 18:30: Session 1: C++23 – What’s in it for You? (Marc Gregoire)
    C++23, the next release of the C++ standard, introduces new features to the core language and to the Standard Library. This session will bring you up to date with the latest features coming with this new release.
    The session includes core language topics such as consteval if statements, multidimensional subscript operators, decay copy, unreachable code, and more. New Standard Library features that will be shown include monadic operations for std::optional, std::flat_map, std::flat_set, a stacktrace library, changes to the ranges library, improvements to std::format, std::expected, and many more.
  • 19:30: Break
  • 19:45: Session 2: Emscripten, what and how? (Dave De Breuck)
    This talk will give a brief introduction of Emscripten itself and explain how Emscripten, an LLVM-based compiler, converts C++ to JavaScript, which lets you run a C++ codebase on the web at near-native speed.
  • 20:45: Introduction to Twikit, followed by a drink.

The event is free for everyone, but you need to register for it.

There are 60 seats available for this event.

Note: The deadline for registrations is May 9th, 2023!

Share

C++23: Multidimensional Subscript Operator

When you have a class representing multidimensional data, providing access to a specific element in this multidimensional space is often done by providing an operator() with as many parameters as there are dimensions. You had to resort to operator(), because operator[] only supported a single index.

That’s now history, as C++23 introduces the multidimensional subscript operator. Providing such an operator for your class is straightforward:

import std;

template <typename T>
class Matrix
{
public:
	Matrix(std::size_t rows, std::size_t cols)
		: m_rows{ rows }, m_cols{ cols }
	{
		m_data.resize(rows * cols);
	}

	T& operator[](std::size_t x, std::size_t y) { return m_data[x + y * m_cols]; }

private:
	std::size_t m_rows;
	std::size_t m_cols;
	std::vector<T> m_data;
};

The class can be tested as follows:

const std::size_t Rows{4};
const std::size_t Cols{4};
Matrix<int> m{ Rows, Cols };
std::size_t counter{ 0 };
for (std::size_t y{ 0 }; y < Rows; ++y)
{
	for (std::size_t x{ 0 }; x < Cols; ++x)
	{
		m[x, y] = ++counter;
	}
}

for (std::size_t y{ 0 }; y < Rows; ++y)
{
	for (std::size_t x{ 0 }; x < Cols; ++x)
	{
		std::print("{} ", m[x, y]);
	}
	std::println("");
}
Share

C++23: “Hello World!” with Modern C++23

Whenever you learn a new programming language, the first program you write is often a “Hello World!” program that simply prints out “Hello World!” to the console. Up to now, for C++, this usually was something along the following lines:

#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
}

This code snippets imports the required header, <iostream>, and uses std::cout to output text to the standard console.

With modern C++23, this simple program looks quite a bit different:

import std;

int main()
{
    std::println("Hello World!");
}

What has changed?

  • Instead of including the exact headers that are required, you simply import a single named module, std, provided by the standard.
  • Instead of using std::cout, stream insertion operators, and std::endl, you simply use std::println().

Unfortunately, at the time of this writing, there are no compilers yet supporting all the above new features, but soon there will be.

For the time being, if your compiler doesn’t support the named module std yet, you can simulate it yourself by writing your own named module called std. You can do this by writing a code file called std.cppm with the following contents:

export module std;

export import <iostream>;

You can extent this std.cppm named module with whatever header you need in your program.

Secondly, if your compiler does not support std::println() yet, you can simulate it with your own print module in a print.cppm file, e.g.:

export module print;

import <string_view>;
import <iostream>;
import <format>;

namespace std
{
    export template <typename... Args>
    void println(std::string_view sv, Args&&... args)
    {
        std::cout << std::vformat(sv, std::make_format_args(args...)) << std::endl;
    }
}

Warning: this is a very basic simulation of std::println(), which does not support all the features of the real std::println()!.

Share

Next BeCPP UG Meeting Planned for January 17th, 2023

I’m happy to announce that the next meeting of the Belgian C++ Users Group is planned for Tuesday January 17th, 2023 at 18:00 at Medicim / Envista.

Medicim / Envista ( https://www2.medicim.com/ ) is sponsoring this event by providing the location, drinks and catering.

The agenda is as follows:

  • 18:00: Reception with food.
  • 18:30: Session 1: Constraints and Concepts (Peter Van Weert)
    This presentation consists of two parts:
    First, I explain how you can, and more importantly should, constrain your template arguments using requires clauses and, typically, concepts. Doing so results in more readable compilation errors, self-documenting template definitions, and easier, more expressive template specialization.
    Next, I show how you can define your own concepts using the new syntax introduced in C++20. Quite some new syntax to learn, for sure, but you’ll quickly see that constraints and concepts are far easier to master (or at least understand) than the more advanced SFINAE techniques (std::enable_if_t, std::void_t, …) they essentially supersede.
  • 19:30: Break
  • 19:45: Session 2: Space Invaders: The Spaceship Operator is upon us (Lieven de Cock)
    Before C++20 we had to write 6 comparison operators for our user defined types (or even more). For sure, a tedious task. All this gets simplified with the introduction of the spaceship operator.
    What happens to your code, when you turn on “-std=c++20” even before we go near the spaceship operator, did your build break? Why does that happen? We will first investigate, another new feature of C++20, the rewriting rules, and how that impacts your code base. And then we will dissect the spaceship operator, from a using perspective, and from an implementation perspective. Oh yes, comparison categories, what are those, and why are these important with respect to the spaceship operator? All will be answered, what initially looked so simple, does require some extra thoughts it seems to correctly use the tool.
  • 20:45: Introduction to Medicim / Envista, followed by a drink.
Professional C++, 5th Edition


We will be giving away a copy of Professional C++, 5th Edition.

The event is free for everyone, but you need to register for it.

There are 75 seats available for this event.

Note: The deadline for registrations is January 13th, 2023!

Share

Slides of My Presentation at CppCon 2022

This year at CppCon 2022, I gave the following session:

  • “C++23 What’s In It For You?”

You can find the slides of the session below.

Share

Next BeCPP UG Meeting Planned For June 28th, 2022

After a long break due to the Corona pandemic, I’m happy to announce that the next meeting of the Belgian C++ Users Group is planned for Tuesday June 28th, 2022 at 18:00 at Vandewiele Experience Center.

Vandewiele ( https://www.vandewiele.com/ ) is sponsoring this event by providing the location, drinks and catering.

Vandewiele Logo

The agenda is as follows:

  • 18:00: Reception with food.
  • 18:30: Session 1: Minimal Logging Framework in C++ 20 (Koen Poppe)
    As developers, adding log statements seems easy and risk-free. However, with hundreds of machines, collecting those log files can become quite a challenge, let alone making sense of the important information they contain. We set out to reduce logging to its essentials and explore optimisations not only in terms of disk space, but also runtime performance and even exposure. Leveraging ideas from well-known software related workflows, we will write a C++20 logging framework from scratch and highlight some surprises along the way.
  • 19:30: Break
  • 19:45: Session 2: Modern CMake (Lieven de Cock)
    CMake is a cross-platform open source software for managing the build process in a portable fashion. We will have a look at the basics of modern CMake. The old, pre modern cmake way should be considered obsolete, for very good reasons. We will talk about targets, build types, dependencies, usage specifications, …, a quick look on cross compilation, and using 3rd party libraries.
  • 20:45: Introduction to Vandewiele, followed by a drink.

Professional C++, 5th Edition
We will be giving away 2 copies of Professional C++, 5th Edition.

The event is free for everyone, but you need to register for it.

There are 30 seats available for this event.

Note: The deadline for registrations is June 22th, 2022!

Share

Slides of My Presentation at CppEurope 2022 – A Look Ahead At C++23

On 24th of May 2022 I gave the following session at CppEurope 2022:

  • “A Look Ahead At C++23”

You can find the slides of the session below.

Here’s the official announcement slide:

Share

Microsoft Visual C++ STL is C++20 Feature Complete

Microsoft just announced that their STL included with Visual C++ is now C++20 feature complete. This is the case for:

  • Visual Studio 2022 version 17.2
  • Visual Studio 2019 version 16.11.14

All C++20 features are now available under the /std:c++20 compiler flag.

You can read the full announcement here.

Share

C++20: enum class and using Declarations

C++11 has given use strongly-typed enumeration types which are recommended to be used over the old style enumeration types. The problem with the latter is that the enumerators of such old-style enumeration types are automatically exported into the enclosing scope. This can cause naming collisions with names already defined in the enclosing scope. This is also the reason why enumerators of such types are often prefixed with some label to try to make sure they are unique. For example:

enum Color { ColorRed, ColorGreen, ColorBlue };

The strongly-typed enumeration types from C++11 do not automatically export their enumerators to the enclosing scope.

Let’s look at an example. The following defines and uses a strongly-type enumeration type called Color:

enum class Color { Red, Green, Blue };
Color someColor = Color::Green;

To use the enumerators of the Color enumeration type, you need to fully qualify them with Color::. This can become a bit cumbersome if you, for example, need to have a switch statement on the different enumerators:

switch (someColor) {
    case Color::Red:
        // ...
        break;
    case Color::Green:
        // ...
        break;
    case Color::Blue:
        // ...
        break;
}

Since C++20, you can use a using declaration to avoid having to fully qualify all the different enumerators in the switch cases:

switch (someColor) {
    using enum Color;

    case Red:
        // ...
        break;
    case Green:
        // ...
        break;
    case Blue:
        // ...
        break;
}

Of course, it is recommended to have the scope of the using declaration as small as possible, otherwise you again introduce the risk of having naming collisions. That’s why the using declaration in the earlier example is inside the scope of the switch statement.

My book, Professional C++, 5th Edition, explains all new C++20 features, and much more.

Share

Recording of my CppCon 2021 Session “A Crash Course in Calendars, Dates, Time, and Time Zones”

On October 27th, 2021 I gave a presentation titled “A Crash Course in Calendars, Dates, Time, and Time Zones” at CppCon 2021.
You can find the slides here.

The official video is now also available on YouTube. Enjoy 🙂

Share

C++20: Templated Lambda Expressions

Templated lambda expressions allow you to get easy access to type information of generic lambda expression parameters. This also allows you to put constraints on the types of generic lambda expressions parameters. For example, the following generic lambda expression accepts two parameters, both defined with auto type deduction:

[](const auto& a, const auto& b) { /* ... */ }

Since both parameters are auto type deduced, the compiler is free to choose different types for both parameters. If you do not want this, you can use the following templated lambda expression:

[]<typename T>(const T& a, const T& b) { /* ... */ }

This can be combined with C++20 concepts to further constrain type T. For example, you can require that type T is an integral type as follows (needs <concepts>):

[]<std::integral T>(const T& a, const T& b) { /* ... */ }

Using a templated lambda expression, you have direct access to the type of a parameter. Hence, it’s easier to, for example, create a local variable in your lambda expression that has the same type as the type of one of the parameters. Without a templated lambda expression, you would have to involve decltype() and std::decay_t on generic lambda expression parameters to achieve the same thing.

For example, suppose you have a lambda expression accepting a std::vector where the type of the elements can be anything. We can use a generic lambda expression for this. However, if in the body of the lambda, you want to know the exact type of the elements in the vector, you need to use decltype() and decay_t:

[](const auto& v) {
	using V = std::decay_t<decltype(v)>; // vector's type
	using T = typename V::value_type;    // vector element's type
	T temp;
	/* ... */
}

With a templated lambda expression you can make this much more concise and easier to understand:

[]<typename T>(const std::vector<T>& v) {
	T temp;
	/* ... */
}

My book, Professional C++, 5th Edition, explains all new C++20 features, and much more.

Share

C++20: std::span – A View on a Continuous Sequence of Data

std::span, defined in <span>, allows you to handle a sequence of continuous data, without having to worry about where the data is actually stored. For example, suppose you have a function to print the elements of a std::vector:

void print(const std::vector<int>& values)
{
    for (const auto& value : values) { std::cout << value << " "; }
    std::cout << std::endl;
}

This function requires as argument a reference to a std::vector<int>. If you also want to print elements of a C-style array, then you can add a second overload of the print() function, for example:

void print(const int values[], size_t count)
{
    for (size_t i{ 0 }; i < count; ++i) { std::cout << values[i] << " "; }
    std::cout << std::endl;
}

With these two overloads, you can call your print() function with either a reference to a std::vector<int> or with a C-style array. If you want to support other containers, then you can add even more overloads. With std::span, it is possible to write a single function that can work with all kinds of sequential data. Instead of the previous two overloads, you can just write the following single function:

void print(std::span<const int> values)
{
    for (const auto& value : values) { std::cout << value << " "; }
    std::cout << std::endl;
}

Note that a span basically just contains a pointer to the first element in the sequence and the number of elements in the sequence, and never copies the underlying data. Hence, a span is very cheap to copy and is usually passed by value, just as std::string_view.

This single print() function accepting a std::span can be called for continuous data stored in std::vectors, std::arrays, C-style arrays, and more. Here are some examples:

std::vector v{ 1, 2, 3 };
print(v);                   // Pass a vector.

std::array a{ 4, 5, 6, 7 };
print(a);                   // Pass a std::array.
print({ a.data() + 1, 2 }); // Pass part of a std::array.

int ca[]{ 8, 9, 10 };
print(ca);                  // Pass a C-style array.

std::span s{ v };           // Construct a span from a vector.
print(s);                   // Pass a std::span.
print(s.subspan(1, 2));     // Pass part of a std::span.

The output of this code snippet is as follows:

1 2 3
4 5 6 7
5 6
8 9 10
1 2 3
2 3

Tip: If you write a function accepting a const vector<T>&, I recommend considering to accept a span<const T> instead. This allows your function to work with all kinds of continuous data, independent of where the data is actually stored.


My book, Professional C++, 5th Edition, explains all new C++20 features, and much more.

Share

C++20: Seemingly Unexpected Behavior with Date Arithmetic

C++20 has added support for calendars, dates, and time zones to the C++ Standard Library. This also allows you to perform arithmetic with dates. However, certain arithmetic might give seemingly the wrong results.
Let’s look at a simple example that works as expected:

using namespace std::chrono;
auto timestamp1 = sys_days{ 2022y / January / 19d } + 8h + 39min + 42s;
auto timestamp2 = timestamp1 + days{ 3 }; // Add 3 days
std::cout << timestamp1 << '\n' << timestamp2;

The output is as expected:

2022-01-19 08:39:42
2022-01-22 08:39:42

Now let’s try to add 1 year to timestamp1:

auto timestamp3 = timestamp1 + years{ 1 }; // Add 1 year
std::cout << timestamp1 << '\n' << timestamp3;

The output now is:

2022-01-19 08:39:42
2023-01-19 14:28:54

The date part is correctly incremented with 1 year, but the timestamp looks wrong on first sight. However, this is correct according to the C++ standard. The reason why it is seemingly off has to do with support for leap years. The C++ standard states that adding 1 year to a date must add 1 average year to keep leap years into account. So, while you could expect adding 1 year adds 86,400 * 365 = 31,536,000 seconds (86,400 = number of seconds in a day), it doesn’t. Instead, it adds 86,400 * ((365 * 400) + 97) / 400) = 31,556,952 seconds.

The reason why it is behaving like this is that the type of timestamp1, 2, and 3 is std::chrono::time_point which is a so-called serial type, i.e., it represents a date as a single number relative to a certain epoch (= clock origin). If you don’t want this behavior you can perform the arithmetic with a field-based type. The serial-based types above can be converted to a field-based representation as follows:

// Split timestamp1 into "days" and "remaining seconds".
sys_days timestamp1_days = time_point_cast<days>(timestamp1);
seconds timestamp1_seconds = timestamp1 - timestamp1_days;

// Convert the timestamp1_days serial type to a field-based year_month_day.
year_month_day ymd2 = timestamp1_days;

// Add 1 year.
year_month_day ymd3 = ymd2 + years{ 1 };

// Convert the result back to a serial type.
auto timestamp4 = sys_days{ ymd3 } + timestamp1_seconds;
std::cout << timestamp1 << '\n' << timestamp4;

The output now is:

2022-01-19 08:39:42
2023-01-19 08:39:42
Share

Slides of My Presentation at CppCon 2021

This year at CppCon 2021, I gave the following session:

  • “A Crash Course in Calendars, Dates, Time, and Time Zones”

You can find the slides of the session below.

Share

Next BeCPP UG Meeting Planned For June 24th, 2021

The next meeting of the Belgian C++ Users Group is planned for Thursday June 24th, 2021 at 18:00 and will be held online through Microsoft Teams.

The agenda is as follows:

  • 18:00: The Teams meeting will start to give people plenty of time to join.
  • 18:30: Session 1: A new way of formatting in C++20, are we getting there in the end? (Lieven de Cock)
    We will have a look at the problems the (s)printf family has brought upon us for decades, how iostreams tries to solve this in an unfriendly way, and how we can now have the best of both worlds, with the upcoming C++20 std::format (or for now with the reference implementation fmt::format).
  • 19:30: Session 2: Understanding value categories in C++ (Kris van Rens)
    In C++ today, do you know what an xvalue is? Or a prvalue? Why would you want to know? Because it matters! In C++, each expression is characterized by a value category. These value categories are used to describe parts of the C++ standard, and are often used in books and articles. You might have heard of terms like ‘lvalue’ or ‘rvalue’, which are the most commonly known ones. Over the years, changes to the C++ language changed the meaning of value categories. This means a lot of information about value categories is outdated or just plain wrong. In this talk, I will explain what expression value categories are in today’s C++ standard. It turns out that knowledge about value categories can really be beneficial. Not only will it enrich your understanding of C++ in general, it will deepen your understanding of mechanisms like move semantics. Also, it can help you to make better choices about code. These choices can then leverage language rules to enable compilers to generate efficient code without redundant copies. Other, related topics that will be covered: copy elision, return value optimization, temporary materialization.

The event is free for everyone, but you need to register for it.

There are 300 seats available for this event.

Note: The deadline for registrations is June 23rd, 2021!

Share

“Professional C++, 5th Edition” Released

After working on it for a year, I’m proud to announce my new book “Professional C++, 5th Edition” is finished 🙂

It has been updated to the C++20 standard and uses certain C++20 features, such as modules and std::format(), throughout all examples.

It is published by Wiley/Wrox, and available on Amazon.

Official Description

Improve your existing C++ competencies quickly and efficiently with this advanced volume

Professional C++, 5th Edition raises the bar for advanced programming manuals. Complete with a comprehensive overview of the new capabilities of C++20, each feature of the newly updated programming language is explained in detail and with examples. Case studies that include extensive, working code round out the already impressive educational material found within. 

Without a doubt, the new 5th Edition of Professional C++ is the leading resource for dedicated and knowledgeable professionals who desire to advance their skills and improve their abilities. This book contains resources to help readers: 

  • Maximize the capabilities of C++ with effective design solutions  
  • Master little-known elements of the language and learn what to avoid  
  • Adopt new workarounds and testing/debugging best practices  
  • Utilize real-world program segments in your own applications 

Notoriously complex and unforgiving, C++ requires its practitioners to remain abreast of the latest developments and advancements. Professional C++, 5th Edition ensures that its readers will do just that. 

Share

Book “C++ Lambda Story”

A friend of mine published a new book, titled “C++ Lambda Story”. The book explains everything you need to know about lambda expressions in C++.

The book guides you through the evolution of C++ Lambda Expressions so that you can learn it step by step. It starts with C++03 and a motivation to have “ad-hoc” functors, and then moves into the latest C++ standards:

  • C++11 – early days of the feature. You’ll learn about all the essential aspects of lambdas and several tricks you might apply. This is the longest chapter as it needs to cover a lot of topics.
  • C++14 – updates. See how to use generic lambdas and captures with an initializer.
  • C++17 – more improvements, especially by handling ‘this’ pointer and allowing ‘constexpr’. You’ll also learn about the overloaded pattern and how to derive from lambda.
  • C++20 – in this section you’ll see all of the new features adopted for C++20 like template lambdas and how to use them with concepts and constexpr algorithms.

Additionally, throughout the chapters, you’ll learn about the following techniques:

  • Immediately Invoked Functional Expressions (IIFE)
  • How to instrument a default functor to gather extra information
  • Replacing std::bind1st, std::bind2nd and removed functional stuff
  • The Overloaded Pattern and how to inherit from a lambda
  • Passing C++ captureless lambda as a function pointer to C API
  • LIFTING with lambdas
  • Storing lambdas in a container
  • Variadic templates and arguments packs
  • Lambdas and asynchronous execution
  • and many more

The author, Bartłomiej Filipek, is giving a 28% discount to readers of my blog. This offer is valid until 27th of January 2021.
Follow this link to use the coupon if you are interested in this offer.

Share

Book “Beginning C++20”

My friend Peter Van Weert finished a new edition of “Beginning C++20” for which I was technical editor. It’s a great book to learn C++20 for programmers new to C++ and those who may be looking for a refresh primer on C++ in general.

Here is the abstract:

Begin your programming journey with C++ , starting with the basics and progressing through step-by-step examples that will help you become a proficient C++ programmer. This book includes new features from the C++20 standard such as modules, concepts, ranges, and the spaceship operator. All you need are Beginning C++20 and any recent C++ compiler and you’ll soon be writing real C++ programs. There is no assumption of prior programming knowledge.

All language concepts that are explained in the book are illustrated with working program examples, and all chapters include exercises for you to test and practice your knowledge. Free source code downloads are provided for all examples from the text and solutions to the exercises.

This latest edition has been fully updated to the latest version of the language, C++20, and to all conventions and best practices of modern C++. Beginning C++20 also introduces the elements of the C++ Standard Library that provide essential support for the C++20 language.

What You Will Learn:

  • Begin programming with the C++20 standard
  • Carry out modular programming in C++
  • Work with arrays and loops, pointers and references, strings, and more
  • Write your own functions, types, and operators
  • Discover the essentials of object-oriented programming
  • Use overloading, inheritance, virtual functions, and polymorphism
  • Write generic function and class templates, and make them safer using concepts 
  • Learn the ins and outs of containers, algorithms, and ranges
  • Use auto type declarations, exceptions, move semantics, lambda expressions, and much more
Share