目前网上的教程大都是bind类成员函数,基本上加上this后和bind普通函数差别不大。
很少遇到bind类成员模板函数的,我也是在阅读buttonrpc_cpp14源码时遇到的相关用法。不得不佩服buttonrpc_cpp14的作者,把c++11和C++14的新功能玩得真叫一个6。
话不多说直接上代码:
class ThreadObject
{
public:
typedef std::function<void(std::string)> tTask;
public:
ThreadObject() {}
~ThreadObject() {}
public:
template<typename F, typename S>
auto mybind(F f, S s)
{
//m_handlers[name] = std::bind(&buttonrpc::callproxy<F, S>, this, func, s, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
return std::bind(&ThreadObject::callproxy<F, S>, this, f, s, std::placeholders::_1);
}
template<typename F, typename S>
inline void callproxy(F fun, S s,int t)
{
callproxy_(fun, s,t);
}
template<typename R, typename C>
void callproxy_(R r,C c,int t) {
(c->*r)(1, "2", 3);
}
private:
tTask m_task;
};
int main() {
ClassMem s1;
ThreadObject obj;
auto a = obj.mybind(&ClassMem::bar, &s1);
a(2);
return 0;
}