All posts in "Expressive code"

How to Write Expressive Class Definitions

Published October 30, 2020 - 0 Comments

As developers, we read a lot of code. A typical code reading task is to scan through a class definition in a header file, in order to understand what the class is about. Sometimes, the purpose of the class does not appear as clearly as we would like. Sometimes, we need to spend a bit […]

An Attempt to Write Fallbacks With Expressive Code

Published October 23, 2020 - 0 Comments

When you need to initialise a value out of several possible choices and take the first valid one, the code can get verbose pretty quickly. Let’s take an example, inspired from a piece of legacy code I saw once. We start with a simple case, where we need to assign a value from one specific […]

The differences between tie, make_tuple, forward_as_tuple: How to Build a Tuple in C++?

Published October 16, 2020 - 0 Comments

Tuples are handy C++ components that appeared in C++11, and are a very useful help when programming with variadic templates. To make things even simpler, C++ offers not one but three helpers to build tuples and make our variadic template code more expressive: std::make_tuple, std::tie and std::forward_as_tuple. All three reflect in their name the fact that they put […]

How to Implement operator= When a Data Member Is a Lambda

Published October 2, 2020 - 0 Comments

In C++, some types of class members make it tricky to implement a copy assignment operator, operator=. For example references, const members, and… lambdas. Indeed, in the majority of cases, lambdas don’t have an operator=. (In case you’re wondering in what case lambdas have an operator=, it is in C++20 and when they don’t capture anything.) […]

What std::exchange does, and how to remember it

Published September 18, 2020 - 0 Comments

std::exchange was introduced in the C++ standard library in C++14 under the header <utility>. Its name suggests that it’s a general-purpose and useful function, and its template prototype working with any type confirms this impression. I don’t know about you, but I always had a problem with std::exchange: I couldn’t remember what it was doing. I […]

Use Private Inheritance to Restrict Interfaces

Published August 28, 2020 - 0 Comments

This is a guest post from Federico Kircheis. Federico is a (mainly C++) developer in Berlin, always looking how to improve himself, and finding interesting problems to solve. Federico is the author of the viral post Function Poisoning in C++. Inheritance is a form of code reuse and does not necessarily indicate a relationship between […]

A Universal Reference Wrapper

Published June 26, 2020 - 0 Comments

This is a guest post by Ábel Surányi. Ábel is working as a software engineer in the IT security industry. He likes generic and functional programming, especially building abstractions by translating an idea to code in a way that the compiler can understand and catches errors during compilation. You can find Ábel on LinkedIn or on his […]

A Generic Component for Out-of-line Lambdas

Published June 12, 2020 - 0 Comments

When exploring out-of-line lambdas, we saw how we could make a call site using a lambda more expressive by hiding the lambda in a separate function. We transformed this code that shows low-level details: auto const product = getProduct(); std::vector<Box> goodBoxes; std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), [product](Box const& box) { // low-level details const double volume = […]

Out-of-line Lambdas

Published June 5, 2020 - 0 Comments

Lambdas are a great tool to make code more expressive. Except when they aren’t. With C++11 bringing them to the language, we were given the liberating power to create anywhere those little functions embarking bits of context. Sometimes they make our code terse and to the point. But sometimes, they sit in the middle of […]

1 3 4 5 6 7 20