Articles & Books

Defining Interfaces in C++ with ‘Concepts’ (C++20) -- Daniel Lemire

In an earlier blog post, I showed that the Go programming language allows you to write generic functions once you have defined an interface.

Defining Interfaces in C++ with ‘Concepts’ (C++20)

by Daniel Lemire

From the article:

Java has a very similar concept under the same name (interface). I gave the following example:


	type IntIterable interface {

	    HasNext() bool

	    Next() uint32

	    Reset()

	}

	

	func Count(i IntIterable) (count int) {

	    count = 0

	    i.Reset()

	    for i.HasNext() {

	        i.Next()

	        count++

	    }

	    return

	}

Consider Using constexpr Static Function Variables for Performance in C++ -- Daniel Lemire

When programming, we often need constant variables that are used within a single function.

Consider Using constexpr Static Function Variables for Performance in C++

by Daniel Lemire

From the article:

When programming, we often need constant variables that are used within a single function. For example, you may want to look up characters from a table. The following function is efficient:

char table(int idx) {
  const char array[] = {'z', 'b', 'k', 'd'};
  return array[idx];
}

ISO C++ Feb 2023 meeting trip report (core language) -- Jason Merrill

The C++ committee had its second hybrid meeting in Issaquah, WA in February, to finalize C++23. This was also the venue where we finished up C++14.

ISO C++ Feb 2023 meeting trip report (core language)

by Jason Merrill

From the article:

The C++ committee had its second hybrid meeting in Issaquah, WA in February, to finalize C++23.  This was also the venue where we finished up C++14.  I was there in person from Red Hat, while Thomas Rodgers and Jonathan Wakely attended virtually.  As usual, I spent most of my time in the Core working group.

The primary goal of the meeting was to finish responding to national body comments on the draft and advance the result for balloting to become C++23, and indeed we did; the week went smoothly from my perspective.  We also spent a fair amount of time reviewing papers and issues that were not expected to go into C++23.

Most of the C++23 fixes at this meeting were unremarkable, but a couple are worth mentioning:

CWG2518 clarifies that...

Functional Programming in C++ -- John Carmack

A classic, over a decade old and worth making the rounds again:

Functional Programming in C++

by John Carmack

From the article:

My pragmatic summary: A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in. ... No matter what language you work in, programming in a functional style provides benefits.  You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient.

... C++ doesn’t encourage functional programming, but it doesn’t prevent you from doing it, and you retain the power to drop down and apply SIMD intrinsics to hand laid out data backed by memory mapped files, or whatever other nitty-gritty goodness you find the need for. ... 

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.