Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Queue of functions

Tags:

c++

So I have multiple methods with different arguments:

class c;

void c::foo1(int a) {

}

void c::foo2(int a, int b) {

}

How do I have a queue/vector of these functions? It doesn't necessarily have to be an std::function object, but I need a way to enqueue the execution of functions.

like image 857
grande_arminho Avatar asked Mar 01 '14 03:03

grande_arminho


1 Answers

Your question is a bit vague, but if you already know the target object and the arguments to the call at the time you insert the functions into the queue, you can use a queue of parameterless lambdas:

std::deque<std::function<void()>> q;
c x;
q.push_back( [&x] { x.foo1(1); } );
q.push_back( [&x] { x.foo2(2,3); } );
// ... some time later we might want to execute the functions in the queue ...
while (!q.empty())
{
    q.front()();
    q.pop_front();
}
like image 134
fredoverflow Avatar answered Oct 16 '22 20:10

fredoverflow