[Edit] I was just told that libFuzzer is deprecated. I think the ideas presented are valid for any fuzzer, but the details will differ. When I mentioned fuzzing a data structure, I was asked some questions like "how does one actually do that?", and "how does fuzzing work?" This article intends to bring a very brief introduction to explain what is going on and how to use it. Making sophisticated…
Suppose that the logic of your program depends on time. That is, you need to keep track of when something in the past happened, and what time it is now, and the logic of what to do depends on how much time passed between that previous event and now. There are many programs with this kind of behaviour. My experience is primarily from networking, where we need to figure out if a response is timely…
I've seen this a few times too many recently, and need to get it off my chest. Ponder a class that has a private member function. The function does not touch any member variables nor does it call any member functions. In my opinion, there are three possible situations here: The function is completely generic. Nothing it does is specific to the class. The function is specific to the problem domain…
A Linux C++ programmers adventure in improving Windows CI on GitHub actions TL;DR; Ninja is available directly in the windows images Use ilammy/msvc-dev-cmd@v1 to set up the MSVC environment for building with Ninja. You need nested quotes for CXX flags from CMake CLI options. How it started: It has been very frustrating, on and off, for a few days now, and I thought that if I write down my…
Some time ago, I received a bug report that, in short, said "your class does not work with CRTP". I was very confused by this statement. First a short recap. CRTP , the Curiously Recurring Template Parameter idiom (more often referred to as Curiously Recurring Template Pattern), a C++ construction where a class inherits from a template instantiated with itself. A simple, but admittedly somewhat…
The performance of std::visit came up in a discussion, and my first thought was that from reading generated assembly code, it's a jump table, so it should be pretty fast. But then it dawned on me, all examples I've seen have been jumps to functions. The compiler has not been able to inline the visitor. This is, of course, of no consequence if your visitor does any work worth speaking of, but if…
A former colleague of mine recently described the steps to speak at a conference as: * Write a proposal and (optionally) a talk outline. * Get accepted by the conference organisers. * Write the talk. * Deliver the talk. Cool! This is not wrong, by the way. But... how do you get accepted? I'm sorry, but I don't have any great advice here, but I can tell you what little I know from my limited…
Now and then, I find myself writing something like if (x == a || x == b || x == c) ... , and every time the repetition of x == annoys me. A number of people, me included, have reduced the repetition by writing code like: 1 2 3 4 5 6 7 8 9 10 template < typename T > class is { public: constexpr is(T t_) : t(std :: move(t_)) {} template < typename ... U > constexpr auto any_of(U const & ... u) const…
A flat map is a data structure that maps a key to a value, and that stores the data in a compact flat memory structure to get better cache hit rate. They can be substantially faster than hash tables and trees, for provided the data sets are small, but how small? There's also more than one way to implement a flat map, and what are the pros and cons of the alternatives? I set out to compare 4…
So I've written about compile time quick sort twice before ( 2011 and 2015 ,) but now when C++17 support is becoming available, I thought I'd try it again. I'll be taking advantage of std::integral_constant<type, value> a lot. It has the advantage that it encodes the value in the type directly, while still allowing arithmetics as if it was a constant of the type. Unfortunately, it's rather much to…
Yesterdays post about Generating lambdas for clarity and performance showed how to make use of higher order functions to improve clarity while giving the optimiser a chance to improve performance, but the example used retained the original inflexible design. Here's a short recap. Given a struct Employee, there is a function that filters out developers based on their salary. The original code…
Higher order functions , functions that operate on other functions or returns functions, are familiar to those who have had some experience with functional programming, but they often seems magical to those who have not. Some of those with experience of using higher order functions have a gut feeling that they are expensive to use and prefer to avoid them. [ edited 2017-01-08: added performance…
Serializing data in C++ is a surprisingly difficult problem. There are many libraries for it with varying degrees of finesse, power and ease of use. C++17 offers an unexpected simplification with structured bindings . The simplification does not lead to a universal solution, but it's applicability is wide none the less. First, what is this serialization problem? It may be the need to transfer data…
Recently I stumbled upon a question by @arne_mertz of Simplify C++ fame (if you don't read that blog, start now!) about using string literals as types. In 2013 I wrote about strings as types , and the technique used works, but it's not exactly elegant. The problem is to get from "string literal" , to something like 1 2 3 4 5 6 7 template < char ... c > class String { ... }; String < 's' , 't' ,…
We've all experienced them, the long and unhelpful compilation errors from templates, usually referring to some internal header you didn't even know existed. Finding the source of the error can be painful, and not unusually the clue, if there is any, is some where in the middle of the long list of messages. Yes yes, concepts are coming to C++. GCC 6 has them, and they are in a TS . Concepts can…
We've all hand crafted comparison operators for structs with many members, and we've all cursed the tedium. It's all right for equality comparison, but lexicographical ordering relations is a different story when there are more than two members. Hopefully all C++ developers have by now learned about the std::tie() -idiom. 1 2 3 4 5 6 7 8 9 10 11 12 struct S { int a; int b; int c; }; bool operator…
I must begin with saying that if you found this because you have a performance problem, you should almost certainly look elsewhere. It is highly unlikely that your performance problem is caused by your priority queue. If, however, you are curious, or you have done careful profiling and found out that the cache characteristics of your priority queue are causing your performance problem, and you…
[ edit 2015-Jun-7 : The source code is available on github ] When writing unit tests, you typically don't care much about execution speed, but compile time performance is important. After all, if building your unit test program takes 3 minutes, Test Driven Development becomes so painful it requires super human determination, but if the build takes 3 seconds, TDD becomes a joy. I became interested…
A contender for the most useless program ever written just got a much needed overhaul. In 2011 I wrote about compile time quick sort as a challenge to myself, and as an exercise in the then newfangled variadic templates. Now, having worked with C++11 for a few years, and lately also C++14, I see that the code I wrote then is unnecessarily clumsy and highly unidiomatic. In C++14 the data…
As previously introduced , the Trompeloeil C++ framework is a new mocking framework for C++14. If you're not at all familiar with Trompeloeil , you may want to read the introductory first. Quick Recap Before diving in to the topic of sequence control, here comes a brief recapitulation of Trompeloeil . You need to define a mock class, and you do that as you define any class, but with mock…
Trompeloeil is a new mocking framework for C++, aimed at ease of use without sacrificing expressive power. In arts, trompeloeil is intended to mock your mind, making you believe you see something that isn't what it appears to be. In unit tests, we use use mocks to fool the unit under test, so that we can break dependencies and test small pieces in isolation. Trompeloeil requires a reasonably C++14…
Sometimes when crafting an interface, we want to ensure that some illegal constructs leads to compilation errors. After all, a good interface is easy to use correctly, and difficult to get wrong, and what can be more difficult to get wrong than something that doesn't compile? We also know that untested often is buggy, or at least we cannot be sure that it is correct, and tests tests that aren't…
As odd as it may seem, I have more than once felt the need to express a string literal as a unique type which can be used as a any class. As an example, I would like to inherit from a string literal: class C : public "literal" { ... }; C++11 sort of provides the means with the introduction of variadic templates . Here's an introduction with a simple t_string (for type string,) with easy access to…
At times it's desirable to give a message at compile time. Sounds cheezy, eh? Well read on and find out. As an example of the cheezy kind, the compile-time quick sort shown here earlier contained an unnecessary run time element with a main() function, for_each() and a print template. It is possible to display all information at compile time by causing a compiler error, like this: template…
A lot has been written about new features coming in the 2011 revision of ISO C++. One that has received surprisingly little attention is the <chrono> header. The types and functions therein makes it so much simpler to write time keeping software. Its neat simple interface is, however, also problematic. Here's a small example program that shows both neatness and problems: #include <chrono> #include…