lundi 4 décembre 2017

Put in a queue functions with different arguments C++11

I am working in C++11. I try to create a queue that takes variadic arguments. The first approach that I tried is the following

#include <iostream>
#include <functional>
#include <queue>
using namespace std;
std::queue<std::function<void()>> funcs;
class ThePool
{
    public:
    template<class... Args>
    void Add_Job(function<void( Args&& ... arg)> New_Job)
    {

    }
   template<typename _Callable, typename... _Args>
   void QueueFunction(_Callable&& __f, _Args&&... __args)
   {
         std::function<void()> func = std::bind(std::forward<_Callable 
         (__f), std::forward<_Args>(__args)...);
        funcs.push(func);
    }
    void print(int i )
    {
       cout << "Hello Word"<< i <<endl;
   }

 };
int main(int argc, char** argv) {
    ThePool a;
    a.QueueFunction(&ThePool::print,a, 5);
    std::function<void()> func = funcs.back();
    func();
    return 0;
}

But I would like to pass arguments in the functions that are bind using the placeholders. For example I like to pass more that one type of functions. For example I would like to pass a void function with one argument or two arguments and e.t.c How I should modify the QueueFunction method and the funcs queue in order to support something like this? For example use bind like this std::bind(&QueueFunction ::print, &a, std::placeholders::_1); but in more generic format

Best regards George

Aucun commentaire:

Enregistrer un commentaire