Kiedyś to dawno powrót do utworzenia puli wątków wtyczki w C++; Ponieważ funkcja brał trzy parametry można napisać jak to
Załóżmy metodę ma podpis:
int CTask::ThreeParameterTask(int par1, int par2, int par3)
Aby utworzyć obiekt funkcyjny do wiązania trzy parametry można zrobić tak
// a template class for converting a member function of the type int function(int,int,int)
//to be called as a function object
template<typename _Ret,typename _Class,typename _arg1,typename _arg2,typename _arg3>
class mem_fun3_t
{
public:
explicit mem_fun3_t(_Ret (_Class::*_Pm)(_arg1,_arg2,_arg3))
:m_Ptr(_Pm) //okay here we store the member function pointer for later use
{}
//this operator call comes from the bind method
_Ret operator()(_Class *_P, _arg1 arg1, _arg2 arg2, _arg3 arg3) const
{
return ((_P->*m_Ptr)(arg1,arg2,arg3));
}
private:
_Ret (_Class::*m_Ptr)(_arg1,_arg2,_arg3);// method pointer signature
};
Teraz, aby powiązać parametry, musimy napisać funkcję wiążącą. Tak, o to idzie:
template<typename _Func,typename _Ptr,typename _arg1,typename _arg2,typename _arg3>
class binder3
{
public:
//This is the constructor that does the binding part
binder3(_Func fn,_Ptr ptr,_arg1 i,_arg2 j,_arg3 k)
:m_ptr(ptr),m_fn(fn),m1(i),m2(j),m3(k){}
//and this is the function object
void operator()() const
{
m_fn(m_ptr,m1,m2,m3);//that calls the operator
}
private:
_Ptr m_ptr;
_Func m_fn;
_arg1 m1; _arg2 m2; _arg3 m3;
};
, a funkcję pomocnika użyć klasy binder3 - bind3:
//a helper function to call binder3
template <typename _Func, typename _P1,typename _arg1,typename _arg2,typename _arg3>
binder3<_Func, _P1, _arg1, _arg2, _arg3> bind3(_Func func, _P1 p1,_arg1 i,_arg2 j,_arg3 k)
{
return binder3<_Func, _P1, _arg1, _arg2, _arg3> (func, p1,i,j,k);
}
i tu nam jak to nazwać
F3 f3 = PluginThreadPool::bind3(PluginThreadPool::mem_fun3(
&CTask::ThreeParameterTask), task1,2122,23);
Uwaga: f3(); wywoła metodę task1-> ThreeParameterTask (21,22,23);
Dla bardziej drastyczne szczegóły ->http://www.codeproject.com/Articles/26078/A-C-Plug-in-ThreadPool-Design
także przydatny dla wywołania zwrotne do funkcji składowych: 'myThread = boost :: thread (boost :: bind (& MyClass :: threadMain, this))' – rlduffy
Ładne wyjaśnienie wiązania. Ale co z 'std :: function'? – RedX
Twój przykład 'pow' nie kompiluje się. Ponieważ 'pow' jest przeciążoną funkcją, musisz ręcznie określić, które przeciążenie. Wiązanie nie może pozostawić go do wyprowadzenia przez wywołującego wynikowego funktora. Na przykład. 'std :: transform (vec.begin(), vec.end(), out.begin(), std :: bind ((double (*) (double, int)) std :: pow, _1, 7)) ; ' –