C++,
Template
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.
RSS | GitHub |
by Bjørn Reese
C++,
Template
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
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
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
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++
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++
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++
Affine objects can be used at most once.
We are going to build an affine function object that can only be invoked once.
C++
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
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 ofstd::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
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
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
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 theerror_code
from Boost to make it compatible with C++11. This enables us to usestd::error_code
throughout our entire project, and still integrate with third-party libraries that useboost::system::error_code
.First we have to learn how to define custom error codes.
C++,
Template
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
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
Three-valued logic has three states:
- True
- False
- Unknown or indeterminate
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.