Comparing std::functions for equality?

前端 未结 7 516
自闭症患者
自闭症患者 2020-12-03 04:47

How can I compare two C++11 std::functions with operator==, and return true if both of said functions refer to the same f

7条回答
  •  心在旅途
    2020-12-03 04:58

    What about comparing two shared_ptr?

    using MessageFilter = std::function;
    
    static void onMessageReceived(const int msgID)
    {
        std::cout << "msg id => " << msgID << std::endl;
    }
    
    static void someFunc()
    {
        auto filter = std::make_shared(&onMessageReceived);
    
        if (filter && *filter)
        {
            (*filter)(1234);
        }
    }
    

    As you can see, 'filter' is a shared_ptr, so it is easy to compare with another.

提交回复
热议问题