Compile-time implementation
In most embedded applications, we know a lot about the system’s behavior at compile time. This means that when using the Observer pattern, we already know all the subscribers. If we assume that subscribers are only registered once and never unregistered, we can create a compile-time version of the Observer pattern.
To enable this, we’ll first break down the key C++17 features that make compile-time implementation feasible.
Leveraging variadic templates
We will base the implementation on variadic templates. We will start with a simplified implementation to explain variadic templates, parameter packs, and fold expressions – C++ features that will allow us to create a compile-time version of the Observer pattern. Let us proceed with the following code:
#include <cstdio>
struct display {
static void update(float temp) {
printf(“Displaying temperature %.2f \r\n”, temp);
}
};
struct data_sender...