Jonathan Boccara's blog

Passing Booleans to an Interface in an Expressive Way

Published May 4, 2018 - 14 Comments

In order to allow a function to behave in several different way, and to allow its caller to choose amongst these behaviours, we have several tools at our disposal. Plenty, actually. There are various sorts of polymorphisms embedded in the language such as virtual functions and templates. And we’ve also seen that a caller can […]

How to Use Tag Dispatching In Your Code Effectively

Published April 27, 2018 - 7 Comments

Constructors lack something that the rest of the functions and methods have in C++: a name. Indeed, look at the following code: class MyClass { public: MyClass(); void doThis(); void doThat(); }; void doSomethingElse(MyClass const& x); Every routine has a name that says what it does, except for the constructor, which only bears the name […]

Make Your Containers Follow the Conventions of the STL

Published April 24, 2018 - 8 Comments
conventions STL

One day I had to do a little refactoring that consisted in renaming a method called getSize() into size(), because I needed to pass its class to generic code that expected a method size(). And what made this refactoring a little special is that this class was used very widely across a pretty large codebase. This is not something […]

How to Reorder A Collection With the STL

Published April 20, 2018 - 5 Comments
permutations STL rotate

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 […]

How to Pass a Polymorphic Object to an STL Algorithm

Published April 17, 2018 - 3 Comments
polymorphic object stl C++

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++ […]

Moving Ranges Around with STL Algorithms

Published April 13, 2018 - 5 Comments
Moving ranges

We’ve seen various ways to achieve complex operations on ranges with STL algorithms along the posts of the STL Learning Resource. Let’s now see how to just move collections around. A much simpler topic… …or is it? Heaving ranges around There are essentially 3 STL algorithms that allow to move several elements of a collection in […]

Which One Is Better: Map of Vectors, or Multimap?

Published April 10, 2018 - 11 Comments
map vector multimaps C++

While advising on how to make code more expressive on the SFME project, I came across an interesting case of choosing the right data structure, which I’ll share with you with the permission of the authors of the projects. We had to associate a key with several values, and perform various operations. Should we use […]