Search code examples
c++boostc++98boost-bind

Generalize the argument passed to boost::bind


I am trying to write a generic struct MyCounter which uses boost::bind . What I am trying to do is to write MyCounter such a way that by changing its template argument type, I could bind to different methods. I was under the impression that if I pass something like MyThread::send as a template argument, I will be able to use it directly in bind(). Apparently I was wrong. Could you kindly help me with the alternatives? Thank you

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <vector>
#include <utility>

struct MyThread //simplified
{
    void send1()
    {
        std::cout << "In Send1\n";
    }
    void send2()
    {
        std::cout << "In Send2\n";
    }
};

struct Job //ignore me
{
    Job(const unsigned int interval)
        : _tChk(0)
        , _tInterval(interval)
    {}

    void operator()(unsigned long nowSec)
    {
        if (nowSec > _tChk)
        {
            _functor();
            _tChk += nowSec + _tInterval;
        }
    }

private:
    unsigned long  _tChk;
    const unsigned int  _tInterval;

protected:
    boost::function<void()> _functor;
};

struct JobInvoker //ignore me
{
    typedef boost::shared_ptr<Job> JobPtr;
    JobInvoker(MyThread &myThread)
        : _myThread(myThread)
    {}

    template<typename JOB>
    void addJob(const unsigned int interval)
    {
        JobPtr job = boost::make_shared<JOB>(interval, _myThread);
        _jobs.push_back(job);
    }

    void operator() (JobPtr job)
    {
        (*job)(_now.tv_sec);
    }

    void invoke(timeval now)
    {
        _now = now;
        std::for_each(_jobs.begin(), _jobs.end(), *this);
    }

    timeval _now;
    std::vector<JobPtr> _jobs;
    MyThread &_myThread;
};

//my struggle
template<typename F>
struct MyCounter : public Job
{
    MyCounter(const unsigned int interval, MyThread &myThread)
    : Job(interval)
    {
        _functor = boost::bind(&F, &myThread); //<-- Error
    }
};

int main()
{
  MyThread t;
  JobInvoker jobInvoker(t);
  jobInvoker.addJob<MyCounter<MyThread::send1> >(2); //<-- Error
  jobInvoker.addJob<MyCounter<MyThread::send2> >(2); //<-- Error
  timeval now;//...
  jobInvoker.invoke(timeval now)
  return 0;
}

UPDATE: compiled with: g++ boost_f.cpp -lboost_system

we get the following errors:

boost_f.cpp: In constructor ‘MyCounter<F>::MyCounter(unsigned int, MyThread&)’:
boost_f.cpp:81:34: error: expected primary-expression before ‘,’ token
         _functor = boost::bind(&F, &myThread); //<-- Error
                                  ^
boost_f.cpp: In function ‘int main()’:
boost_f.cpp:89:46: error: type/value mismatch at argument 1 in template parameter list for ‘template<class F> struct MyCounter’
   jobInvoker.addJob<MyCounter<MyThread::send1> >(2); //<-- Error
                                              ^
boost_f.cpp:89:46: error:   expected a type, got ‘MyThread::send1’
boost_f.cpp:89:51: error: no matching function for call to ‘JobInvoker::addJob(int)’
   jobInvoker.addJob<MyCounter<MyThread::send1> >(2); //<-- Error
                                                   ^
boost_f.cpp:89:51: note: candidate is:
boost_f.cpp:52:10: note: template<class JOB> void JobInvoker::addJob(unsigned int)
     void addJob(const unsigned int interval)
          ^
boost_f.cpp:52:10: note:   template argument deduction/substitution failed:
boost_f.cpp:89:51: error: template argument 1 is invalid
   jobInvoker.addJob<MyCounter<MyThread::send1> >(2); //<-- Error
                                                   ^
boost_f.cpp:90:46: error: type/value mismatch at argument 1 in template parameter list for ‘template<class F> struct MyCounter’
   jobInvoker.addJob<MyCounter<MyThread::send2> >(2); //<-- Error
                                              ^
boost_f.cpp:90:46: error:   expected a type, got ‘MyThread::send2’
boost_f.cpp:90:51: error: no matching function for call to ‘JobInvoker::addJob(int)’
   jobInvoker.addJob<MyCounter<MyThread::send2> >(2); //<-- Error
                                                   ^
boost_f.cpp:90:51: note: candidate is:
boost_f.cpp:52:10: note: template<class JOB> void JobInvoker::addJob(unsigned int)
     void addJob(const unsigned int interval)
          ^
boost_f.cpp:52:10: note:   template argument deduction/substitution failed:
boost_f.cpp:90:51: error: template argument 1 is invalid
   jobInvoker.addJob<MyCounter<MyThread::send2> >(2); //<-- Error
                                                   ^
boost_f.cpp:92:29: error: expected primary-expression before ‘now’
   jobInvoker.invoke(timeval now)

Solution

  • Like the error says, a member function isn't a type, so it can't be used for a template type parameter.

    Instead, you need to use a non-type parameter:

    template<void(MyThread::*F)()>
    struct MyCounter : public Job
    {   
        MyCounter(const unsigned int interval, MyThread &myThread)
        : Job(interval)
        {
            _functor = boost::bind(F, &myThread);
        }
    };
    

    Live Example

    Note that I made F a pointer to a member function of MyThread instead of a typename.