Articles & Books

On Writing Functions That Accept Any Specialization of a C++ Template Type -- Raymond Chen

RaymondChenPic.pngSuppose you want to write a template function that accepts any specialization of std::vector? What may work today may not work tomorrow.

On Writing Functions That Accept Any Specialization of a C++ Template Type

by Raymond Chen

From the article:

Suppose you want to write a template function that accepts any specialization of std::vector. Your first try would probably be something like this:

template<typename Value>
void accept_any_vector(std::vector<Value> v);

However, this does not actually accept any vector specialization. There is a second template argument for the allocator, which has a default value that nearly everyone uses. But if somebody has a vector with a custom allocator, then your function won’t match.

Dealing with Mutation: Thread-Safe Interface -- Rainer Grimm

dealingwithmutations.pngI continue my journey with concurrency patterns in today's post. The Thread-Safe Interface fits very well when the critical sections are just objects.

Dealing with Mutation: Thread-Safe Interface

by Rainer Grimm

From the article:

The naive idea to protect all member functions of a class with a lock causes, in the best case, a performance issue and, in the worst case, a deadlock.

A Deadlock

The small code snippet has a deadlock.

struct Critical{
    void memberFunction1(){
        lock(mut);
        memberFunction2();
    ...
}

void memberFunction2(){
        lock(mut);
        ...
    }

    mutex mut;
};

Critical crit;
crit.memberFunction1();

Calling crit.memberFunction1 causes the mutex mut to be locked twice. For simplicity reasons, the lock is a scoped lock. Here are the two issues:

60 terrible tips for a C++ developer

In this article, you're going to find 60 terrible coding tips — and explanations of why they are terrible. It's a fun and serious piece at the same time. No matter how terrible these tips look, they aren't fiction, they are real: we saw them all in the real programming world.

60 terrible tips for a C++ developer

by Andrey Karpov

From the article:

Terrible tip N46. Write your code as if the chairman of the IOCCC judges will read it and as if they know where you live (to come and give you the prize).

It's a reference to a quote — "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live". This is John F. Woods's phrase, however, it's sometimes credited to Steve McConnell who quoted it in his "Code Complete" book.

Into the Extreme – Fold-Expressions -- Coral Kashri

CoralKashri.pngFold expressions exist in C++ since C++17 and significantly affect how we treat variadic templates. Back in the day, I wrote about fold-expressions as part of the metaprogramming series, but today we will explore the extreme cases of fold-expression usages.

Into the Extreme – Fold-Expressions

by Coral Kashri

From the article:

In the case of a unary fold (fold expression without initialization), this case is legal for 3 types of operators: &&||, and ,.

Operator &&

template <typename ...Args>
auto and_cond(Args... args) {
    return (args && ...);
}

In case of empty parameters (for the call and_cond()), the function will return true. A reasonable explanation for this decision might be that && operator requires there won’t be any part that evaluates false. In this case, there are no parts at all, so none of the parts evaluates false, and therefore the result should be...

Dealing with Mutation: Locking -- Rainer Grimm

dealingwithmutationlocking.pngLocking is a straightforward idea to protect a critical section. A critical section is a section of code that, at most, one thread can use at any time.

Dealing with Mutation: Locking

by Rainer Grimm

From the article:

Scoped locking is the idea of RAII applied to a mutex. Scoped locking is also known as synchronized block and guard. The key idea of this idiom is to bind the resource acquisition and release to an object’s lifetime. As the name suggests, the lifetime of the object is scoped. Scoped means that the C++ run time is responsible for object destruction and, therefore, for releasing the resource.

The class ScopedLock implements Scoped Locking.

The Case of the Two Billion Characters Long String -- Giovanni Dicanio

Several times you need to pass data (including text) from cross-platform C++ code to platform-specific C++ code. If you don't pay enough attention, weird bugs can happen at the boundary.

The Case of the Two Billion Characters Long String

by Giovanni Dicanio

From the article:

What is going on here? What is the origin of this bug? Why is the string reported as being more than 2 billion characters long?

Let's try to solve this mystery, with a sprinkle of assembly language, too!

 

Storage duration and Non-local Objects in C++ -- Bartlomiej Filipek

Filipek-book.pngC++ allows us to declare various forms of non-local objects: they usually live throughout the execution of the whole program. In this article, we’ll look at global variables, dynamic, and thread-local objects. We’ll also consider new features for safe initialization C++20.

Storage duration and Non-local Objects in C++

by Bartlomiej Filipek

From the article:

To start, we need to understand two key properties of an object in C++: storage and linkage. Let’s begin with the definition of storage, from [basic.stc#general]:
"The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object."
An object in C++ has one of the following storage duration options:

Bitwise Binary Search: Elegant and Fast -- Orson Peters

peters-bitwisebinarysearch.png

I recently read the article Beautiful Branchless Binary Search by Malte Skarupke. In it they discuss the merits of the following snippet of C++ code implementing a binary search.

Bitwise Binary Search: Elegant and Fast

by Orson Peters

From the article:

In this article I will provide an alternative implementation based on similar ideas but with a very different interpretation that is (in my opinion) incredibly elegant and clear to understand, at least as far as binary searches go. The resulting implementation also saves a comparison in almost every case and ends up quite a bit smaller.

Dealing with Sharing -- Rainer Grimm

temp_file_DealingwithSharing-Grimm1.pngIf you don’t share, no data races can happen. Not sharing means that your thread works on local variables. This can be achieved by copying the value, using thread-specific storage, or transferring the result of a thread to its associated future via a protected data channel.

Dealing with Sharing

by Rainer Grimm

From the article:

The patterns in this section are quite obvious, but I will present them with a short explanation for completeness. Let me start with Copied Value. If a thread gets its arguments by copy and not by reference, there is no need to synchronize access to any data. No data races and no lifetime issues are possible.

Data Races with References

The following program creates three threads. One thread gets its argument by copy, the other by reference, and the last by...

Concurrency Patterns -- Rainer Grimm

There are many well-established patterns used in the concurrency domain. They deal with synchronization challenges such as sharing and mutation but also with concurrent architectures. Today, I will introduce and dive deeper into them in additional posts.

Concurrency Patterns

by Rainer Grimm

From the article:

The main concern when you deal with concurrency is shared, mutable state or, as Tony Van Eerd put it in his CppCon 2014 talk “Lock-free by Example”: “Forget what you learned in Kindergarten (ie stop Sharing)”. A crucial term for concurrency is a data race. Let me first define this term.

  • Data race: A data race is when at least two threads access a shared variable simultaneously. At least one thread tries to modify the variable. If your program has a data race, it has undefined behavior. This means all outcomes are possible, so reasoning about the program makes no sense anymore.

A necessary condition for a data race is a mutable, shared...