Articles & Books

How to Use C++20 Modules with Bazel and Clang--Ryan Burn

Modules are coming.

How to Use C++20 Modules with Bazel and Clang

by Ryan Burn

From the article:

Modules are a feature added to C++20 that aims to provide better encapsulation and faster build times. While modules are not fully supported by compilers and probably not ready for use in production code, Clang’s support is fairly usable.

In this guide, I’ll show you how to use modules with Clang and the Bazel build system by making use of the project github.com/rnburn/rules_cc_module.

Let’s look at it works on a simple hello world program...

constexpr if--Rainer Grimm

Have you ever used it?

constexpr if

by Rainer Grimm

From the article:

In today's post, I want to introduce a very interesting C++17 feature: constexpr if. constexpr if enables it to conditionally compile source code and can also be used for nice tricks at compile time...

Why 4 Bloomberg engineers wrote another C++ book -- Tech At Bloomberg

This article features an interview to the four authors of the newly released "Embracing Modern C++ Safely" book, motivating the surprising decision to analyze C++11 and C++14 in-depth in 2021, and discussing the contents and style of the publication.

Why 4 Bloomberg engineers wrote another C++ book

Tech At Bloomberg

From the article:

But why do we need yet another C++ book? After all, one of the book’s principal authors, John Lakos, has already written two. And why now? Why Bloomberg? Who is this for? But first, why modern C++? [...] The authors limited the book to features with which they have accumulated more than five years of experience. The current edition is an authoritative summary of C++11 and C++14 features, and future editions are planned to cover C++17 and C++20.

(Note that "Embracing Modern C++ Safely" is on sale (up to 55% off) until February 26.)

On finding the average of two unsigned integers without overflow--Raymond Chen

How did you solve it?

On finding the average of two unsigned integers without overflow

by Raymond Chen

From the article:

Finding the average of two unsigned integers, rounding toward zero, sounds easy:

unsigned average(unsigned a, unsigned b)
{
    return (a + b) / 2;
}

However, this gives the wrong answer in the face of integer overflow: For example, if unsigned integers are 32 bits wide, then it says that average(0x80000000U, 0x80000000U) is zero...

Technique: Compile Time Code Generation and Optimization--Jonathan Müller

Have you ever done that?

Technique: Compile Time Code Generation and Optimization

by Jonathan Müller

From the article:

C++ constexpr is really powerful. In this blog post, we’ll write a compiler that can parse a Brainfuck program given as string literal, and generate optimized assembly instructions that can then be executed at runtime. The best part: we neither have to actually generate assembly nor optimize anything ourselves! Instead we trick the compiler into doing all the hard work for us.

The same technique can be used whenever you want to specify some sort of “program” in a different way and translate it at runtime: regexes, routing tables, etc.

Improving Stability with Modern C++, Part 5 — Revisiting the Rule of Three

Rule of Three

Improving Stability with Modern C++, Part 5 — Revisiting the Rule of Three

by Ralph Kootker

From the article

If the programmer, following the Rule of Three, declares a copy constructor, copy-assignment operator, or destructor, the compiler will not generate any move operations. [...] Declare a move constructor or move-assignment operator only, and now the default copy constructor and copy-assignment operator won't be generated.