-1
是的,这是如何完成的?以一个函数作为参数类似于std :: thread
我抬头的东西有相当具体的定义函数。首先,我不知道如何用va_arg
列表调用函数。对于两个人,我不知道它应该是什么样子。
我希望能够为任何功能做
event.register(func, arg1, arg2, arg3, ...);
,而不使用模板,就像std::thread
做的。
我该怎么做?
是的,这是如何完成的?以一个函数作为参数类似于std :: thread
我抬头的东西有相当具体的定义函数。首先,我不知道如何用va_arg
列表调用函数。对于两个人,我不知道它应该是什么样子。
我希望能够为任何功能做
event.register(func, arg1, arg2, arg3, ...);
,而不使用模板,就像std::thread
做的。
我该怎么做?
解决
class Event
{
list<function<void()>> functions;
public:
Event()
{
} //Event
template<typename T, typename... Args>
void reg(T& func, Args... args)
{
functions.push_back(bind(func, args...));
} //reg
void execute()
{
for (auto& func : functions)
{
func();
} //for
} //execute
};
=)
Event event;
event.reg(test, 2, 2.5);
event.reg(*[](int n, const char* foo){ cout << n << " " << foo << endl; }, 5, "rawr");
event.execute();
那么,你显然已经在这里使用了模板。 – ForNeVeR 2014-10-05 03:03:37
'的std :: thread'做它与可变参数模板ARGS – quantdev 2014-10-05 02:10:07
乘坐['的std :: function'](HTTP:// EN。 cppreference.com/w/cpp/utility/functional/function)参数。 – 2014-10-05 02:11:11
_“不使用模板,就像std :: thread一样。”_正如前面提到的,当然,std :: thread' ***实际上使用了模板***。也看看[完美转发](http://stackoverflow.com/questions/7038357/make-unique-and-perfect-forwarding)。 – 2014-10-05 02:16:25