Articles & Books

string_view odi et amo--Marco Arena

string_view has recently joined the C++ standard and it can dramatically help your daily job. In this article, after introducing how it works, I show and discuss a few common pitfalls I have met in the last years:

string_view odi et amo

by Marco Arena

From the article:

string_view-like wrappers have been successfully used in C++ codebases for years, made possible by libraries like boost::string_ref. I think all of you know that string_view has joined the C++ standard library since C++17...

My take on variant--Jonathan Müller

An interesting point of view and implementation of a variant in one article!

My take on variant

by Jonathan Müller

From the article:

C++17 is going to add std::variant. To quote the linked documentation, it is a “type-safe union”. A union is like a struct, but can only store one member at a time. This has many applications, but sadly it doesn’t mix well with non-trivial types, you have to call the destructor yourself etc. Furthermore, nothing prevents you from accessing a union member that isn’t active.

std::variant fixes that. It correctly calls the destructor when switching the active member, it prevents invalid access, etc. However, I’m not quite happy with it and I needed an implementation now. So I’ve decided to implement my own variant as part of my type_safe library.

It was a fun challenge and since my previous attempt was two years ago, I could improve it a lot. Let’s go through some of my design decisions.

Folding Functions--Sumant Tambe

Let's use those fold operators!

Folding Functions

by Sumant Tambe

From the article:

In the last post we looked at basic usage of C++17 Fold Expressions. I found that many posts on this topic discuss simple types and ignore how folds may be applicable to more complex types as well. In this post I'm going to describe folding over functions...

Emscriptened -- Adi Shavit

The `path` to the web.

Emscriptened!

by Adi Shavit

From the article:

It might be a fun little project to generate a web-based interactive version of the effects of various methods of `filesystem::path`.

 

Return early and clearly--Arne Mertz

How to return well:

Return early and clearly

by Arne Mertz

From the article:

There are different guidelines out there about where and how many return statements to use in a function, e.g. return only once at the end of the function or return early and often. Which one makes for the most readable code?

Building a hybrid spin mutex in C++ -- Foster Brereton

Forster Brereton reports about his first steps to build a hybrid mutex.

Building a hybrid spin mutex in C++

by Foster Brereton

From the article

Blocking Mutexes
A blocking mutex will halt the thread until it acquisition. It is useful because it consumes negligible computer resources while blocked. This leaves the CPU free to perform other tasks, including whatever other task currently owns the mutex. All this goodness is not cheap, however: it takes a decent amount of time to block thread. If your critical section is brief, you could be spending a disproportionate amount of time protecting it instead of running it.
Generally, blocking mutexes should be used when your critical section will take a while, such as I/O operations, calling out to the OS, or doing laundry in a collegiate dorm.


Spinning Mutexes
A spinning mutex will enter into an infinite loop (spin) until acquisition. It is useful because it can resume very quickly once the lock has been obtained, resulting in minimal overhead while protecting a critical section. However, since the thread remains active on the CPU, it can reduce (or eliminate!) the ability of the CPU to do other work††. If your critical section is long, you could be spending a disproportionate amount of time protecting it instead of running it.
Generally, spin mutexes should be used when your critical section is brief, such as reading or writing a memory-resident data structure.

Finding a middle ground
The dichotomy between the two mutex behaviors has left me stuck more than once. What if I was trying to protect a global resource that occasionally required a call to the OS? In those cases a blocking mutex is not a good fit, as modifying the memory-resident structure is pretty quick. However a spin mutex would be equally bad, because I do need to go to the OS time and again, and it would be a pessimization to spike a CPU while doing so.

Quantifiers, metaprogramming and concepts -- Nikos Athanasiou

Quantification expresses the extent to which a predicate is true over a set of elements. This installment describes the use of predicate logic in metaprogramming.

Quantifiers, metaprogramming and concepts

by Nikos Athanasiou

From the article:

Metaprograms often use predicate logic in creative ways. For example, type queries in generic code are used to constrain, dispatch, specialize, activate or deactivate code at compile time.