Articles & Books

How to Insert Several Elements in a Vector (With No Memory Errors)--Jonathan Boccara

How do you do it?

How to Insert Several Elements in a Vector (With No Memory Errors)

by Jonathan Boccara

From the article:

Inserting elements in a vector sounds like the most basic use case we can think of when it comes to using collections in C++.

Nevertheless, this is a complex topic in itself, because std::vector offers various ways to insert several elements. Choosing the most appropriate depending on your exact use case allows to write more expressive code. And misusing the interface of std::vector can lead to memory errors.

Let’s navigate the various ways to insert several elements in a vector in a safe way, so that you can choose the one that fits best for your code...

Compile-time pre-calculations in C++--Mohammad Nasirifar

The evolution.

Compile-time pre-calculations in C++

by Mohammad Nasirifar

From the article:

With C++17’s constexpr functions and C++20’s consteval specifier, it is easy to do io-independent pre-calculations of algorithms while compiling the program. This may not be useful or even possible in long running programs and is unlikely to make a difference in their performance, but in binaries that do short calculations with a set of parameters fixed at compile-time, a Sieve of Eratosthenes array, or roots of unity used for calculating DFT loaded right from the binary could make a difference...

East End Functions--Phil Nash

Did you know about those reasons?

East End Functions

by Phil Nash

From the article:

There has been a recent stirring of attention, in the C++ community, for the practice of always placing the const modifier to the right of the thing it modifies. The practice has even been gifted a catchy name: East Const (which, I think, is what has stirred up the interest)...

Class Templates--Rainer Grimm

The series continue.

Class Templates

by Rainer Grimm

From the article:

A function template represents a family of functions. Accordingly, a class template represents a family of classes. Today, I want to introduce class templates...

Function Templates - More Details about Explicit Template Arguments and Concepts--Rainer Grimm

The series continue.

Function Templates - More Details about Explicit Template Arguments and Concepts

by Rainer Grimm

From the article:

In the last post "Function Templates", I wrote about the overloading of function templates and automatically deducing the return type of a function template. Today, I dive deeper and specify explicitly the template arguments of a function template and bring concepts into the play...

A Default Value to Dereference Null Pointers--Jonathan Boccara

Adding more readability.

A Default Value to Dereference Null Pointers

by Jonathan Boccara

From the article:

With C++17, modern C++ has acquired a nullable object: std::optional. optional has a pretty rich interface, in particular when it comes to handling null optionals.

On the other hand, the oldest nullable type in C++, pointers, doesn’t have any helper to make the handling of its nullity more expressive.

Let’s see what we can do about it, to make our code using pointers, smart or raw, easier to read...