std::vector of std::function

前端 未结 1 1416
梦谈多话
梦谈多话 2021-01-20 14:27

I have the following:

  typedef std::function event_type;

  class Event : boost::noncopyable
  {
  private:
   typedef std         


        
相关标签:
1条回答
  • 2021-01-20 15:14

    You can store boost::function in the vector, provided you don't use std::find. Since you seem to need this, wrapping the function in its own class with equality would be probably the best.

    class EventFun
    {
      int id_;
      boost::function<...> f_;
    public:
      ...
      bool operator==(const EventFun& o) const { return id_==o.id_; } // you get it...
    };
    

    Note that this requires you maintain the id_ in a sane way (eg. two different EventFuns will have different id_s, etc.).

    Another possibility would be to store boost::functions with a tag the client would remember and use to identify the particular function on deleting it.

    0 讨论(0)
提交回复
热议问题