我正在尝试编写一个使用MyCounter
的通用结构boost::bind
。我正在尝试编写MyCounter
的方式,通过更改其模板参数类型,我可以绑定到其他方法。我的印象是,如果我将类似MyThread::send
的内容作为模板参数传递,则可以直接在bind()
中使用它。显然我错了。您能帮我替代方案吗?谢谢
#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;
}
更新:
编译为:
g++ boost_f.cpp -lboost_system
我们收到以下错误:
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)
答案 0 :(得分:3)
就像错误提示一样,成员函数不是类型,因此不能用于模板类型参数。
相反,您需要使用非类型参数:
template<void(MyThread::*F)()>
struct MyCounter : public Job
{
MyCounter(const unsigned int interval, MyThread &myThread)
: Job(interval)
{
_functor = boost::bind(F, &myThread);
}
};
请注意,我使F
成为了MyThread
成员函数的指针,而不是typename
的指针。