boost::bind 实现原理, 手动实现一个

template<typename R, typename T, typename A1>
class hangj_call
{
public:
	hangj_call(R (T::*f_)(A1), const T& u_):f(f_), u(u_){}

	R operator()(A1 a1){ return (u.*f)(a1); }

private:
	R (T::*f)(A1);
	T u;
};

template<typename R, typename T, typename A1>
hangj_call<R, T, A1> hangj_bind(R (T::*f)(A1), const T& u)
{
	return hangj_call<R, T, A1>(f, u);
}


#include <cstdio>

class X
{
public:
	bool f(int a){
		printf(" %d
", a);
		return true;
	}
};

int main(int argc, char** argv)
{
	X x;
	auto func = hangj_bind(&X::f, x);
	func(8);

	return 0;
}

原理如此,其它形式的 bind 都类似

原文地址:https://www.cnblogs.com/hangj/p/8515208.html