Suppose I have an object that is observable by other objects:
struct Object
{
struct Listener
{
virtual void fire() = 0;
}
Object(std::vector<Listener *> &listeners) :
listeners_(listeners)
{}
void fire()
{
for(auto *l : listeners_)
l->fire();
}
private:
std::vector<Listener *> listeners_;
};
Now, I would like to do the same thing using templates. Here's a skeleton of what I mean:
template<typename ... Listeners>
struct Object
{
Object(Listeners&&...listeners)
{
// How do I store each of the differently-typed references?
}
void fire()
{
// How do I iterate over the list of listeners?
}
};
Note that the key thing here is that I'm trying to avoid virtual function calls. I don't want my Listeners (in the templated code)to have to subclass a pure virtual class or anything like that.