Posts

Showing posts from October, 2017

Implementing signals with variadic templates

One of the most popular patterns in modern C++ is the use of signals. Signals are an implementation of the observer pattern, where observers register, or connect themselves to a signal, so that the signal can notify them when it has been triggered. This paradigm has been used in a lot of software, with Qt's signal/slot system probably being the most widely known. The signal is usually called by using operator () and passing a certain number of arguments. Observers usually implement a function that matches the signature of the operator () and when operator () is called, each function of the registered observers is called with the same arguments. Modern C++ allows us to define a generic signal in a very straightforward manner by using variadic templates. Generic means: The signal class is written once and the template instantiation takes care of defining the different parameters and types that may be passed to the signal. In the traditional definition of the observer pattern, a