C++ 将函数作为参数,类似于std::thread

C++ 将函数作为参数,类似于std::thread,c++,C++,是的,这是怎么做到的 我查找的东西有非常具体的函数定义。首先,我不知道如何使用va_arg列表调用函数。对于两个人,我不知道它应该是什么样子 我希望能够做到 event.register(func, arg1, arg2, arg3, ...); 对于任何不使用模板的函数,就像std::thread那样 我该怎么做?已解决 class Event { list<function<void()>> functions; public: Event()

是的,这是怎么做到的

我查找的东西有非常具体的函数定义。首先,我不知道如何使用
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
};
类事件
{
列表功能;
公众:
事件()
{
}//事件
模板
作废登记(T&func,参数…参数)
{
函数。推回(绑定(func,args…);
}//注册
void execute()
{
用于(自动和函数:函数)
{
func();
}//为了
}//执行
};
=)

事件;
事件记录(测试,2,2.5);

event.reg(*[](int n,const char*foo){cout
std::thread
使用可变模板argsTake一个参数。“不使用模板,就像std::thread那样。”正如前面提到的,
std::thread
实际上当然使用模板。另外请看一下。显然,您在这里使用了模板。
Event event;

event.reg(test, 2, 2.5);
event.reg(*[](int n, const char* foo){ cout << n << " " << foo << endl; }, 5, "rawr");
event.execute();