All posts in "Expressive code"

C++ Concepts: More than Syntactic Requirements

Published February 26, 2021 - 0 Comments

After years and years of expectation, concepts have finally made it in C++20. Concepts are descriptions in code of a set of expressions that must be valid with a given type. Those are syntactic requirements. But there is more to concepts than that: concepts also have semantic requirements. Before getting into that, here is a […]

A Recap on string_view

Published February 19, 2021 - 0 Comments

The string capabilities of C++ have little evolved since C++98, until C++17 brought a major evolution: std::string_view. Let’s look at what string_view is about and what it can bring to your code, by making it more expressive and making it run faster. std::string_view As its name suggests, std::string_view is a view on a string. But let’s define […]

How std::any Works

Published February 5, 2021 - 0 Comments

In the previous post we’ve seen a very nice technique to use value semantics with inheritance and virtual methods, which was made possible by std::any. Given its usefulness, it would be interesting to better understand std::any. Indeed, std::any is sometimes said to be “the modern void*“. But it does much more than a void*. A […]

Inheritance Without Pointers

Published January 29, 2021 - 0 Comments

Inheritance is a useful but controversial technique in C++. There is even a famous talk by Sean Parent called Inheritance is the base class of evil. So inheritance is not the most popular feature of the C++ community. Nevertheless, inheritance is useful, and widely used by C++ developers. What is the problem of inheritance? It […]

Infix Function Calls with Boost HOF

Published January 8, 2021 - 0 Comments

In C++, functions are called with a prefix syntax. This means that at call site, the function name is before the parameters: myFunction(parameter1, parameter2); ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ function parameters On the other hand, binary operators such as operator+ are called with an infix syntax, which means that the operator is between the parameters: parameter1 + parameter2 Some […]

Include What You Use

Published January 1, 2021 - 0 Comments

I’ve used the clang based include-what-you-use tool on a fairly large chunk of code — a couple of hundreds of files, containing dozens of includes each. That was an interesting experiment. Here are my takeaways on this powerful tool, what it can bring to your code, and a few things I wish I had known […]

On Design Patterns in C++

Published December 18, 2020 - 0 Comments

Design patterns are a must-know in programming today. The first reference to “design patterns” I know of is the famous GoF book: This book is a classic of programming and sits on the desk of many programmers across the world. The Design patterns described in this book are various ways to structure code to solve […]

Auto for Types, but Not for Concepts

Published December 4, 2020 - 0 Comments

AAA. Three letters that the C++ community associates to the early times of Modern C++. AAA. Almost Always Auto. Is this still valid today, now that C++20 is the latest standard? Exploring the reasons behind the AAA guideline allow to better understand auto and what it can express in our code. Especially since the guideline […]

How Lambdas Make Function Extraction Safer

Published November 13, 2020 - 0 Comments

One of the most interesting talks I saw when I was at CppCon 2019 was also one of the shortest ones. During one of the lightning talks evenings, Ezra (a.k.a. eracpp) demonstrated a technique to extract some code from a long function in a systematic way. Long functions are common in C++ legacy code, and extracting […]

How to Make a Copyable Object Assignable in C++

Published November 6, 2020 - 0 Comments

Some types in C++ have a copy constructor that doesn’t have the same semantics as their assignment operator (operator=). Take references, for example. References can be copied: int i = 42; int& r1 = i; int& r2 = r1; // r2 now points to i, like r1 But it doesn’t do the same thing as […]