Posts

A reinterpret_cast Trap

January 22, 2017

Sometimes, casting is just inevitable. And then there’s even not much science behind it, at least it seems so. Once some address is provided in a variable of the right size, a typed pointer can be casted out of it, and then the object can be accessed via its members and methods as usual. In some situations it is really easy to get the casting wrong, leading to interesting bugs. This article describes an example situation and a proper fix.
Read more


Python Style printf for C++ with pprintpp

November 5, 2016

The C++ STL comes with stream style character output, which is an alternative to the classic printf like format function collection of the C library. For different reasons, some C++ programmers still stick to printf like formatting. This article demonstrates the pprintpp (open source, and available on Github) library, which tries to make printf use comfortable and safe, while avoiding any runtime overhead.
Read more


Do Not Fear Recursion

September 11, 2016

There are a lot of algorithms which can be implemented using recursive or iterative style. Actually, everything can be implemented in both styles. For a lot of algorithms, the recursive version is simpler to read, write, and understand. But nevertheless, programmers know, that recursive functions burden a lot of memory consumption, because there is usually a call instruction per recursive call, which puts another call frame on the stack. Interestingly, this is not true sor some special cases.
Read more


Wrapping Algorithms into Iterators

September 4, 2016

Sometimes there is the requirement to generate a range of numbers from some algorithm. Be it a simple range of increasing numbers, or only odd numbers, or only primes, or whatever. Some calculations can be optimized by memorizing some values for the calculation of the next number, just as this applies for fibonacci numbers. This article shows how to wrap such calculations into iterators in order to have performant, and nicely encapsulated algorithms.
Read more


Type List Compile Time Performance

June 25, 2016

Soon, after writing my first meta programs with C++ templates, i realized, that certain programming patterns lead to sky rocketing compile times. I came up with rules of thumb like “Prefer pattern matching over if_else_t”, and “Prefer nested type lists over variadic type lists”. But i did not know how much faster which pattern is, i just knew about tendencies. Finally, i sat down to write some compile time benchmarks, and this blog posts presents the results.
Read more


Generating Integer Sequences at Compile Time

June 24, 2016

In some situations, it can be useful ot generate sequences of numbers at compile time. This article shows how to generate integer sequences with C++ templates, and gives an example how to use it.
Read more


Executing Brainfuck at Compile Time with C++ Templates

June 16, 2016

This article completes a series which aims at explaining how to implement a Brainfuck Interpreter, which runs at compile time, because it is a template meta program.
Read more


Implementing a Turing Tape for Use at Compile Time

May 15, 2016

Turing Machines consist of a tape with memory cells, a tape reader like cassette drives and a program table. Implementing the tape drive part with an array and a pointer is a trivial thing to do with imperative programming languages. It becomes more interesting when learning purely functional programming, especially in the context of template meta programming in C++. As a preparation for the next article, i will show how to implement a turing tape based on type lists, usable at compile time.
Read more


Transformations between User Input/Output and Type Lists

May 14, 2016

Type lists are an important way to represent ordered and unordered sets of types at compile time. These types can be real structure- or class types bundling runtime algorithms etc., but they can also convey actual data at compile time. In order to apply certain compile time processing to data, this data needs to be transformed from and to other representations, which can be provided by the programmer and consumed by run time programs. This article shows how to transform back and forth between strings and character type lists.
Read more


Type Lists

May 8, 2016

Homogenuous data in purely functional programs is typically managed in lists. Items can be appended or prepended to lists, different lists can be concatenated. Lists can be filtered, transformed, mapped, reduced, etc. Having all this nice stuff as a template meta library is quite an enabler for complex compile time meta programs.
Read more