Comparing std::tr1::function<> objects

前端 未结 7 1657
粉色の甜心
粉色の甜心 2020-12-07 02:36

I\'ve been trying to implement a C#-like event system in C++ with the tr1 function templates used to store a function that handles the event.

I created a vector so

7条回答
  •  攒了一身酷
    2020-12-07 03:00

    If you are storing function pointers only (and not other functors that match the signature required), this is easy (see code below). But in general, the answer, like other posters have said, is no. In that case, you probably want to store your functors in a hash, as values, with keys being something the user supplies on adding and removing.

    The code below demonstrates how to get the functor/pointer object that is to be called. To use it, you must know the exact type of the object to extract (i.e., the typeid of the type you specify must match the typeid of the contained functor/pointer).

    #include 
    #include 
    
    using std::printf;
    using std::tr1::function;
    
    int main(int, char**);
    static function main_func(&main);
    
    int
    main(int argc, char** argv)
    {
        printf("%p == %p\n", *main_func.target(), &main);
        return 0;
    }
    

提交回复
热议问题