Articles & Books

Affine Space Types

Well defined semantics for positions and displacements.

Affine Space Types

by Adi Shavit

From the article:

I recently came across a geometric structure that deserves to be better known: The Affine Space. In fact, like many abstract mathematical concepts, it is so fundamental that we are all subconsciously familiar with it though may have never considered its mathematical underpinnings.
This post will introduce the Affine Space structure and focus mainly on its role in the C++ type system, the standard library and for creating new strong types.

Refactoring with C++17 std::optional--Bartlomiej Filipek

Isn't it better?

Refactoring with C++17 std::optional

by Bartlomiej Filipek

From the article:

There are many situations where you need to express that something is “optional” - an object that might contain a value or not. You have several options to implement such case, but with C++17 there’s probably the most helpful way: std::optional.

For today I’ve prepared one refactoring case where you can learn how to apply this new C++17 feature...

How to Reorder A Collection With the STL--Jonathan Boccara

Did you know?

How to Reorder A Collection With the STL

by Jonathan Boccara

From the article:

The STL lets you do plenty of things on collections, and one of them is to reorder the elements inside of the collection. Or, said another way, to perform a permutation on the collection.

Inded, moving elements around a collection typically takes a fair amount of complex code to write, involving for loops and iterators. And it is perhaps the area where the STL generates the most spectacular improvements, by encapsulating those complex operations behing meaningful interfaces.

Let’s see what sorts of permutations the STL offers:

  • Lexicographical permutations
  • Cyclic permutations
  • Random permutation
  • Reverse
  • Checking for permutations
  • Other permutations

Quick Q: int a[] = {1,2,}; Weird comma allowed. Any particular reason?

Quick A: For convenience.

Recently on SO:

int a[] = {1,2,}; Weird comma allowed. Any particular reason?

It makes it easier to generate source code, and also to write code which can be easily extended at a later date. Consider what's required to add an extra entry to:

int a[] = {
   1,
   2,
   3
};

... you have to add the comma to the existing line and add a new line. Compare that with the case where the three already has a comma after it, where you just have to add a line. Likewise if you want to remove a line you can do so without worrying about whether it's the last line or not, and you can reorder lines without fiddling about with commas. Basically it means there's a uniformity in how you treat the lines.

Now think about generating code. Something like (pseudo-code):

output("int a[] = {");
for (int i = 0; i < items.length; i++) {
    output("%s, ", items[i]);
}
output("};");

No need to worry about whether the current item you're writing out is the first or the last. Much simpler.

optional in Containers Ⅱ — Not All std::vector Usages Are The Same--Jonathan Müller

What do you think?

optional<T> in Containers Ⅱ — Not All std::vector Usages Are The Same

by Jonathan Müller

From the article:

Okay, so in the previous post I talked about putting optional<T> in container. I came to conclusions which I though were reasonable at the time, however, people — rightfully — pointed out some flaws in my argumentation...

compile-time iteration with C++20 lambdas -- Vittorio Romeo

This article covers various compile-time "iteration" constructs that rely on the upcoming "familiar template syntax for lambdas" C++20 feature.

compile-time iteration with C++20 lambdas

by Vittorio Romeo

From the article:

In this article I'm going to show you how to implement the above constructs, relying on a new nifty addition to C++20 lambdas: [P0428: "Familiar template syntax for generic lambdas"], by Louis Dionne. [...]

It shows how to implement constructs for the following operations:

Iterating over a list of types;
Iterating over a list of compile-time values;
Iterating over a compile-time integral range;
Enumerating a list of types alongside their indices.
The code provided works on g++ 8

How to Pass a Polymorphic Object to an STL Algorithm--Jonathan Boccara

Did you ever try?

How to Pass a Polymorphic Object to an STL Algorithm

by Jonathan Boccara

From the article:

As we can read in the opening chapter of Effective C++, C++ is a federation of 4 languages:

  • the procedural part coming from C,
  • the object-oriented part,
  • the STL part (following a functional programming paradigm),
  • the generic part with templates.

And what’s more, all of those 4 sub-languages are part of one whole: the C++ language. Those 4 paradigms begin united in one language gives opportunities for them to interact – and often, those interactions create interesting situations.

Today we’re focusing on one particular interaction, between the object-oriented model and the STL. There could be multiple forms for this interaction, and the case we will look at is how to pass a polymorphic (that is, having virtual methods) function object to an STL algorithm.

uninitialized_tag in C++--Marius Elvert

Optimise or not?

uninitialized_tag in C++

by Marius Elvert

From the article:

No doubt, C++ is one of those languages you can use to squeeze out every last drop of your CPU’s processing power. On the other hand, it also allows a high amount of abstraction. However, micro-optimization seldom works well with nice abstractions...