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 […]
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 […]
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 […]
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.) […]
This is a guest post from Ben Deane. Ben is a lifelong learner and algorithms fan who works in the finance industry and loves to write expressive, well-performing C++. You can find him on twitter @ben_deane. This blog post has been a long time in the making. I gave a lightning talk on std::exchange at […]
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 […]
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 […]
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 […]
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 = […]
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 […]