我现在最终这样做了-
void invoke(void (f)(), list<any>& params)
{
f();
}
template <typename R>
void invoke(R (f)(), list<any>& params)
{
params.push_front(f());
}
template <typename T0>
void invoke(void (f)(T0), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
f(t0);
}
template <typename R, typename T0>
void invoke(R (f)(T0), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
params.push_front(f(t0));
}
template <typename T0, typename T1>
void invoke(void (f)(T0, T1), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
T1 t1 = any_cast<T1>(*params.begin()); params.pop_front();
f(t0, t1);
}
template <typename R, typename T0, typename T1>
void invoke(R (f)(T0, T1), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
T1 t1 = any_cast<T1>(*params.begin()); params.pop_front();
params.push_front(f(t0, t1));
}
template <typename T0, typename T1, typename T2>
void invoke(void (f)(T0, T1, T2), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
T1 t1 = any_cast<T1>(*params.begin()); params.pop_front();
T2 t2 = any_cast<T2>(*params.begin()); params.pop_front();
f(t0, t1, t2);
}
template <typename R, typename T0, typename T1, typename T2>
void invoke(R (f)(T0, T1, T2), list<any>& params)
{
T0 t0 = any_cast<T0>(*params.begin()); params.pop_front();
T1 t1 = any_cast<T1>(*params.begin()); params.pop_front();
T2 t2 = any_cast<T2>(*params.begin()); params.pop_front();
params.push_front(f(t0, t1, t2));
}
我缺乏 boost::bind 的全部功能——比如,我无法处理方法指针——但我也意识到通过这样做,我有一个堆栈处理器。我可以继续调用对堆栈上的参数进行操作的方法。