Boost logo

Boost Users :

Subject: [Boost-users] [bind] Possible to use variadic templates with bind?
From: Steve Lorimer (steve.lorimer_at_[hidden])
Date: 2010-07-21 10:29:35


Hi

Is it possible to pass variadic template arguments to bind, such that bind
uses them as the placeholder arguments (_1, _2, _3 etc)

I show my use case below.

struct job_queue
{
    // copy f into list for asynchronous envocation by another thread
    void enqueue(boost::function<void()> f);
};
//-----------------------------------------------------------------------

// helper class to define correct form of the member function callback
template<class T, typename... Args> struct cb { typedef void
(T::*type)(Args...); };

// a job defines a callback function which is any member function taking any
number of arguments of any type
template<class T, typename... Args>
class job_t
{
    boost::shared_ptr<job_queue> queue;
    boost::function<void(Args...)> arg_func;
public:
    job_t(typename cb<T, Args...>::type cb, boost::shared_ptr<T> that,
boost::shared_ptr<job_queue> q) :
        arg_func(boost::bind(cb, that, Args...)), queue(q) {} // normally
would be _1, _2, _3, etc

    void post(Args&... args)
    {
        boost::function<void()> f = boost::bind(arg_func, args...);
        queue->enqueue(f);
    }
};



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net