Container for pointers to member functions with different arguments

前端 未结 3 480
南方客
南方客 2021-01-17 05:41

I am looking everywhere (Modern C++ design & co) but I can\'t find a nice way to store a set of callbacks that accept different arguments and operate on different classe

3条回答
  •  广开言路
    2021-01-17 06:19

    Use std::function to store the calls and then use a variadic template argument to forward and bind the function parameters:

    class Clock
    {
        vector> tasks;
    
        template
        void defer(F f, Args&&... args)
        {
            tasks.push_back(bind(f, forward(args)...);
        }
    
    }
    
    void f(A a, B b);
    
    int main()
    {
        A a;
        B b;
    
        Clock c;
        c.push_back(f, a, b);
    }
    

    see also std::bind and std::mem_fun

提交回复
热议问题