I want to create a function like setInterval(func, delay, [arg1, arg2, ...]);
of JavaScript.
void func(int t) {
std::cout<<t<<std::endl;
}
void func(int i, char* t) {
std::cout<<t<<std::endl;
}
class Timer {
public:
void setInterval(auto function, int interval, ...args);
};
void Timer::setInterval(auto function, int interval, ...args) {
while(1) {
function(args);
msleep(interval);
}
}
int main() {
Timer *t1 = new Timer;
t1->setInterval(func, 1000, 2021);
Timer *t2 = new Timer;
t2->setInterval(func, 1000, 2, "hello");
while (1) {
}
return 0;
}
But I don't know how to convert variadic arguments to Class member function. Can anyone help me? Thank you.