RSS GitHub LinkedIn

Under Development

by Bjørn Reese

C++,

Template

18 August 2024

Projected Variadic Fold

Projected variadic fold is a fold over transformed arguments, and is functionally equivalent to std::inner_product but the input is passed as function arguments rather than iterators.

C++,

Template

11 August 2024

Variadic Fold

Variadic fold and partial fold are generalizations of fold expressions. Variadic partial fold produces the intermediate results of a fold. Fold expressions were added to the language in C++17, whereas variadic fold works from C++11 where parameter packs were added.

Folding is useful for efficient numeric computation that requires loop unrolling even when the compiler is optimizing for minimal size.

C++,

Math

21 August 2022

Evaluating Polynomials

Polynomials can be evaluated faster if we rewrite them and common wisdom tells us that a rewrite with fewer operations yields faster performance. For instance, Horner’s rule rewrites the polynomial to use the least number of multiplications. However, this does not yield the fastest performance on modern CPUs with instruction pipelining and vectorization.

C++,

Template

06 March 2022

Deducing Function Signatures

The signature of a function is a type consisting of the return type, the function parameter types, and possibly function qualifiers. The function signature is also known as the function type.

We demonstrate how to create a type traits that works reasonably well for most callable objects.

C++

05 July 2021

Invocation Policy

In C++ we value the zero-overhead principle except when it comes to exceptions. The standard C++ library generally prefers to raise exceptions when passed invalid arguments, and only in a few select cases are such errors treated as undefined behavior to obtain better performance. We are going to build a simple mechanism to let the user have a choice on our classes.

C++

13 June 2021

Obscurable Types

Imagine if we could devise classes whose interface can be turned on or off at specific source code locations. This could be used to build interfaces that are only available inside a transactional scope. Attempting to use the interface outside the transactional scope would result in a compiler error.

C++

06 June 2021

Almost Affine

Affine objects can be used at most once.

We are going to build an affine function object that can only be invoked once.

C++

16 July 2020

The Member Notation

Coding guidelines often introduce a special notation to distinguish member variables from function parameters. This notation is not known to the compiler, so it cannot detect violations. At best we can write rules for static analysis tools, such as clang-tidy, to detect violations.

But there is an alternative supported by the compiler, namely nested classes.

C++,

Error

12 May 2017

Customizing Error Codes

Many C libraries follow the POSIX tradition of returning errors as integers. Some libraries reuse the pre-defined errno values, while others define their own library-specific values.

We show how to integrate the error values of such C libraries with std::error_code. The choice of std::error_code gives us several advantages: (i) errors are type-erased, (ii) errors are propagated without loss of information, and (iii) errors are interoperable.

C++

Network

03 December 2016

Receiving Datagrams

The socket API is defined such that the user supplies a buffer of a pre-determined size that UDP datagrams are read into. If the buffer is smaller than the datagram, then the surplus bytes from the datagram are discarded.

How can we read the entire datagram when we do not know its size in advance?

Statistics

Outliers

07 August 2016

Outliers and Structural Change

Sensors are prone to measurement errors – deviation from expectation. Large deviations can have an adverse impact on on-line processing where irrevocable decisions are made based on partial information. Overreactions can be avoided by removing outliers – deviations larger than an accepted threshold. This makes processing less sensitive to random fluctuation in the measurements.

The downside of outlier removal is that it makes us blind to abrupt structural changes. If the expected level of the measurements suddenly changes, then all subsequent measurements will be discarded as outliers.

We need a way to detect such structural changes.

C++,

Error

18 June 2016

Unifying error codes

C++11 and Boost both have an error_code class, but they cannot be used interchangeably despite their close resemblance. We introduce a trick of re-catogorizing the error_code from Boost to make it compatible with C++11. This enables us to use std::error_code throughout our entire project, and still integrate with third-party libraries that use boost::system::error_code.

First we have to learn how to define custom error codes.

C++,

Template

20 December 2015

Partiality for Functions

While C++ does support partial specialization of templates, it does not do so for function templates. Instead, the general advice is to use function overloading instead. Sometimes that is not a feasible solution though, so we will see how to emulate partial specialization of function template with a some boiler-plate code.

Statistics

26 October 2015

On Average

Calculating the mean of a sample can be done in various ways. A widely used method is the exponential moving average. We will show that this method has a bias towards the initial value, and present a way to remove the bias from the results.

Logic,

Tribool

05 July 2015

Layers of three-valued logic

Three-valued logic has three states:

The unknown state means that we have to device new truth tables for the various logic operators. As there are several different logics of indeterminacy we will end up with different sets of truth tables. This becomes important when we use three-valued logic in composite conditions.





© Bjørn Reese.