c++: std::vector of std::function with arbitrary signatures

后端 未结 2 725
天涯浪人
天涯浪人 2021-01-06 12:45

Is it possible to create an std::vector that can hold an std::function with any signature? (The function arguments would all be pre-bound.)

2条回答
  •  情书的邮戳
    2021-01-06 13:49

    This should work:

    #include 
    #include 
    #include 
    
    void hello() { std::cout << "Hello\n"; }
    void hello2(const char * name) { std::cout << "Hello " << name << '\n'; }
    
    int main()
    {
    
        std::vector> v;
        v.push_back(hello);
        v.push_back(std::bind(hello2, "tim"));
        v[0]();
        v[1]();
    }
    

    How are you doing it?

提交回复
热议问题