(private)
observers
:std::list
or some associative container ofstd::function<R(A...)>
add()/remove()
(ororconnect()/disconnect()
) to manage the content ofobservers
.remove()
would need some form of identification of existing observers, hence the need for an associative container.operator()(A&&... a)
so thatsignal
behaves like a function object. Argumentsa...
are passed without forwarding to underlying functions, so that they are copied if needed, and not invalidated between subsequent calls.additional management like
clear()/empty()
.
Your example usage would be rather like
signal<void(int, int)> s;
s.connect([=](int a, int b){whatever});
s(4, 5);
Isn't that better? Since everything is non-static, the user controls how many instances there are, and names them properly. In fact, a single object may have e.g. ten methods, each with its own observers. Each such method is then a signal and behaves like a function object.