Understand Internals of std::expected -- Bartlomiej Filipek
In the article about std::expected, I introduced the type and showed some basic examples, and in this text, you’ll learn how it is implemented.
Understand Internals of std::expected
by Bartlomiej Filipek
From the article:
In short,
std::expectedshould contain two data members: the actual expected value and the unexpected error object. So, in theory, we could use a simple structure:template <class _Ty, class _Err> struct expected { /*... lots of code ... */ _Ty _Value; _Err _Unexpected; };However, there are better solutions than this. Here are some obvious issues for our “struct” approach.
- The size of the object is the sum of the Value type and the Error type (plus padding if needed).
- Two data members are “active” and initialized, which might not be possible - for example, what if the Value type has no default constructor? The Standard requires that
std::expected" holds either a value of typeTor an error of typeE` within its storage.- We’d have to guarantee that
_Tycannot be a reference type or an array type; it must be aDestructibleType.- Similarly for the
_Errtype we have to guarantee that it’s alsoDestructible, and must be a valid template argument forstd::unexpected(so not an array, non-object type, nor cv-qualified type).- Plus, we’d have to write a lot of code that creates an API for the type

In Qt 4, container classes like QVector introduced an optimization that transformed certain operations on contained objects into efficient byte-level manipulations. By identifying types that can be safely moved via a simple memory copy, Qt was able to streamline reallocations for specific data types like 
Last time, we built a stateful but coalescing update notification using a change counter to identify which request is the latest one, but noted that it does unnecessary work. Let’s see if we can avoid the unnecessary work.
In today's post, I will continue where I left off with last month's post Understanding the role of cv-qualifiers in function parameters. This time, I will focus on type deduction.
The new
You have probably written a class that prints a message in all its special member functions. And like me, you probably wrote it multiple times. I decided to write it well once and for all, and share it.
If you're writing C++, there's a good reason (maybe...) as to why you are. And probably, that reason is performance. So often when reading about the language you'll find all sorts of "performance tips and tricks" or "do this instead because it's more efficient". Sometimes you get a good explanation as to why you should. But more often than not, you won't find any hard numbers to back up that claim. I recently found a peculiar one, the