How to take indefinite function arguments?

I have a simple events system like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Comp {
    typedef void (*cb)();
    unordered_map<string, cb> events;
    void on(string, cb);
    void call(string);
}

void Comp::on(string name, cb f) {
    this->events[name] = f;
}

void Comp::call(string name) {
    if (this->events[name] != nullptr) {
        this->events[name]();
    }
}


But I'm hoping to do something like this: (pseudo code)

1
2
3
4
5
6
7
8
9
10
11
void Comp::call(string name, ...) {
    if (this->events[name] != nullptr) {
        this->events[name](...);
    }
}

c->on("attach", [](int i, bool b, string s){
    // ...
})

c->call("attach", 1, true, "yo");


The idea is each event has different arguments and I can define as many events I want. Is it possible?
example shamelessly stolen from the web:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdarg.h>

double average(int count, ...)
{
    va_list ap;
    int j;
    double tot = 0;
    va_start(ap, count); //Requires the last fixed parameter (to get the address)
    for(j=0; j<count; j++)
        tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
    va_end(ap);
    return tot/count;
}
Topic archived. No new replies allowed.