IIFE for Complex Initialization

What do you do when the code for a variable initialization is complicated? Do you move it to another method or write inside the current scope? In this blog post, I’d like to present a trick that allows computing a value for a variable, even a const variable, with a compact notation.

READ MORE...

Moved or Not Moved - That Is the Question!

C++11 brought Move Semantics. Since then we have extra capabilities to write faster code, support movable-only types, but also more headaches :). At least I have, especially when trying to understand the rules related to that concept. What’s more, we also have copy elision, which is a very common optimisation (and even mandatory in several cases in C++17).

READ MORE...

Lambdas: From C++11 to C++20, Part 1

Lambda expressions are one of the most powerful additions to C++11, and they continue to evolve with each new C++ language standard. In this article, we’ll go through history and see the evolution of this crucial part of modern C++. The second part is available: Lambdas: From C++11 to C++20, Part 2

READ MORE...

The Pimpl Pattern - what you should know

Have you ever used the pimpl idiom in your code? No matter what’s your answer read on :) In this article I’d like to gather all the essential information regarding this dependency breaking technique. We’ll discuss the implementation (const issue, back pointer, fast impl), pros and cons, alternatives and also show examples where is it used.

READ MORE...

C++17 in details: Code Simplification

With each C++ standard, we aim for simpler, cleaner and more expressive code. C++17 offers several “big” language features that should make our code nicer. Let’s have a look. Intro You might say that most of the new language features (not to mention The Standard Library improvements) are there to write simpler/cleaner code.

READ MORE...

C++17 in details: Attributes

“C++ Attributes… what?” There were almost 40% votes like that in my recent Twitter survey. Maybe It would be good to introduce that little-known feature? There’s even a good occasion, as in C++17 we’ll get even more useful stuff connected with attributes. Interested? Intro Have you ever used __declspec, __attribute or #pragma directives in your code?

READ MORE...

Modernize: Sink Functions

One of the guidelines from Modern C++ is to avoid using raw new and delete. Instead, you should use a smart pointer, a container or other RAII object. Today I’d like to focus on so-called ‘sink functions’ that takes ownership of input parameters. How can we modernize code around such calls?

READ MORE...

Const, Move and RVO

C++ is a surprising language. Sometimes simple things are not that simple in practice. Last time I argued that in function bodies const should be used most of the time. But two cases were missed: when moving and when returning a value. Does const influence move and RVO? Intro Just to recall, we’re talking here about using const for variables inside function bodies.

READ MORE...

Variadic Templates and a Factory Function

Variadic Templates from C++11 is probably not a feature that you use on a daily basis. But recently, I’ve come across one refactoring example where I’ve decided to give a try and apply variadics. Intro When I was doing some work in some old UI code I’ve noticed several similar lines of code that looked like that:

READ MORE...

Wrapping Resource Handles in Smart Pointers

Some time ago I covered how to use custom deleters with smart pointers. The basic idea is to specify a dedicated method that will be called when a pointer is released. One logical application of custom deleters might be resource handles like files or the WinApi HANDLE type. Let’s see how can we implement such thing.

READ MORE...