Articles & Books

GCC 13.1 Released

The GCC developers are proud to announce a new major GCC release, 13.1.

GCC 13.1 Released

by Richard Biener

From the announcement:

The C frontend got support for several C23 features, the C++ frontend for C++23 features.  The C++ standard library experimental support for C++20 and C++23 was enhanced.  For the C family of languages you can now use -fstrict-flex-arrays[=level] to control the behavior for the various legacy forms of specifying flexible array members.

What Is the C++ Small String Optimization (SSO)? -- Giovanni Dicanio

What is the Small String Optimization (SSO)?

Let's discover that in the following blog post:

The C++ Small String Optimization

by Giovanni Dicanio

From the article:

How do “Connie” and “meow” differ from “The Commodore 64 is a great computer”?

In several implementations, [...], the STL string classes are empowered by an interesting optimization: The Small String Optimization (SSO).

Model-View-Controller -- Rainer Grimm

The Model-View-Controller (MVC) is one of the classic architectural patterns from the book "Pattern-Oriented Software Architecture, Volume 1". It addresses interactive applications with a  flexible human-machine interface.

Model-View-Controller

by Rainer Grimm

From the article:

The MVC divides the program logic of a user interface into separate components model, view, and controller. The model manages the data and rules of the application. The view represents the data, and the controller interacts with the user.

Purpose

  • User interfaces need to be changed frequently
  • Different user interfaces must be supported
  • The data model is stable

Solution

  • The application is divided into the components Model (data model), View (output components), and Controller (input components)
  • Multiple output components can use the same data model

C++20: consteval and constexpr Functions -- Daniel Lemire

Optimizing compilers seek try to push as much of the computation as possible at compile time.

C++20: consteval and constexpr Functions

by Daniel Lemire

From the article:

In modern C++, you can declare a function as ‘constexpr’, meaning that you state explicitly that the function may be executed at compile time.

The constexpr qualifier is not magical. There may not be any practical difference in practice between an inline function and a constexpr function, as in this example:

inline int f(int x) {
  return x+1;
}

constexpr int fc(int x) {
  return x+1;
}

Functional exception-less error handling with C++23’s optional and expected -- Sy Brand

std::optional has been updated in C++23, and std::expected added as a new way of representing errors in your code. Read more about them here:

Functional exception-less error handling with C++23’s optional and expected

by Sy Brand

From the article:

std::optional<T> expresses “either a T or nothing”. C++23 comes with a new type, std::expected<T,E> which expresses “either the expected T, or some E telling you what went wrong”. This type also comes with that special new functional interface. As of Visual Studio 2022 version 17.6 Preview 3, all of these features are available in our standard library. Armed with an STL implementation you can try yourself, I’m going to exhibit how to use std::optional‘s new interface, and the new std::expected to handle disappointments.

Simple Usage of C++20 Modules -- Victor Zverovich

SimpleUsage-Zverovich.jpgIn my previous post I showed how to compile {fmt} as a C++20 module with clang. Although taking only two commands, ideally it’s not something you should be doing manually. So in this post, I’ll talk about module support in CMake, everyone’s favorite not a build system.

Simple Usage of C++20 Modules

by Victor Zverovich

From the article:

My first attempt was to use the CMake’s built-in functionality advertised in “import CMake; C++20 Modules”. And after some struggle I made it to work with clang but unfortunately it was very limited. Here are some of the problems and limitations that I discovered:

  1. It only worked with ninja and while I don’t have anything against this build system it’s an extra hassle to get this additional dependency installed while make is usually available by default. This restriction also likely means that you cannot use such a CMake config with IDEs.
  2. Native CMake support only worked with clang 16 while the fmt module can be built manually with clang 15.
  3. It required the latest version of CMake and a lot of ceremony to set up, including some incomprehensible things.
  4. There were issues in dynamic dependency extraction both in clang-scan-deps and CMake itself.

 

Functional Exception-less Error Handling with C++23’s Optional and Expected -- Sy Brand

This post is an updated version of an article from five years ago, now that everything Sy talked about is in the standard and implemented in Visual Studio.

Functional Exception-less Error Handling with C++23’s Optional and Expected

by Sy Brand

From the article:

In software things can go wrong. Sometimes we might expect them to go wrong. Sometimes it’s a surprise. In most cases we want to build in some way of handling these misfortunes. Let’s call them disappointments.

std::optional was added in C++17 to provide a new standard way of expressing disappointments and more, and it has been extended in C++23 with a new interface inspired by functional programming.

C++23’s New Fold Algorithms -- Sy Brand

This post explains the benefits of the new “rangified” algorithms, talks you through the new C++23 additions, and explores some of the design space for fold algorithms in C++.

C++23’s New Fold Algorithms

by Sy Brand

From the article:

C++20’s algorithms make several improvements to the old iterator-based ones. The most obvious is that they now can take a range instead of requiring you to pass iterator pairs. But they also allow passing a “projection function” to be called on elements of the range before being processed, and the use of C++20 concepts for constraining their interfaces more strictly defines what valid uses of these algorithms are. These changes allow you to make refactors like:

// C++17 algorithm
cat find_kitten(const std::vector<cat>& cats) {
    return *std::find_if(cats.begin(), cats.end(),
        [](cat const& c) { return c.age == 0; });
}

// C++20 algorithm
cat find_kitten(std::span<cat> cats) {
    return *std::ranges::find(cats, 0, &cat::age);
}