Looking at your code, it looks like you would be better off storing std::function<double(double, int)>
, and then use std::bind
to bind the objects to the member functions.
template <typename F>
void AddSubscriber( const Callable & obj,
F subscriber)
{
Subscribers.push_back(std::bind(subscriber, obj, _1, _2));
}
Or just de-couple Callable
from Caller
completely, by passing something convertible to std::function<double(double, int)>
directly:
class Caller
{
public:
template <typename F>
void AddSubscriber(F&& subscriber)
{
subscribers.emplace_back(std::forward<F>(subscriber));
}
void CallSubscribers()
{
for (const auto& subscriber : subscribers)
{
subscriber(1.0, 2);
}
}
private:
std::vector<std::function<double(double, int)> subscribers;
};
and then do the binding from the caller side.
Caller c;
ClassA a;
using namespace std::placeholders;
c.AddSubscriber(std::bind(&ClassA::CallThis, a, _1, _2));