std ::函数的std :: vector [英] std::vector of std::function

查看:165
本文介绍了std ::函数的std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  typedef std :: function< void(const EventArgs&)>事件类型; 

类事件:boost :: noncopyable
{
private:
typedef std :: vector< event_type> EventVector;
typedef EventVector :: const_iterator EventVector_cit;
EventVector m_Events;

public:
事件()
{
}; //活动

Event(&& _rhs):m_Events(std :: move(_rhs.m_Events))
{
}; // eo mtor

//运营商
事件& operator + =(const event_type& _ev)
{
assert(std :: find(m_Events.begin(),m_Events.end(),_ev)== m_Events.end());
m_Events.push_back(_ev);
return * this;
}; // eo + =

事件& operator = =(const event_type& _ev)
{
EventVector_cit cit(std :: find(m_Events.begin(),m_Events.end(),_ev));
assert(cit!= m_Events.end());
m_Events.erase(cit);
return * this;
}; // eo - =
}; // eo class Event

在编译过程中:

  1> c:\程序文件(x86)\ microsoft visual studio 10.0 \ vc \include\algorithm(41):error C2451:type' void'是非法的
1>表达式的void不能转换为其他类型

现在,我明白这是因为什么是存储在向量和运算符 == 中。是否有另一种方法在STL容器中存储 std :: function ?我需要把它包装在其他东西里吗? 你可以存储 boost :: function ,只要你不使用 std :: find 。因为你似乎需要这样做,所以用自己的类平等地包装函数可能是最好的。

  class EventFun 
{
int id_;
boost :: function< ...> F_;
public:
...
bool operator ==(const EventFun& o)const {return id _ == o.id_; } //你得到它...
};

请注意,这需要您维护 id _ (例如,两个不同的 EventFun s将有不同的 id _ s等)。



另一种可能性是将 boost :: function s与客户端会记住的标签一起存储并用于标识特定功能删除它。


I have the following:

  typedef std::function<void(const EventArgs&)> event_type;

  class Event : boost::noncopyable
  {
  private:
   typedef std::vector<event_type> EventVector;
   typedef EventVector::const_iterator EventVector_cit;
   EventVector m_Events;

  public:
   Event()
   {
   }; // eo ctor

   Event(Event&& _rhs) : m_Events(std::move(_rhs.m_Events))
   {
   }; // eo mtor

   // operators
   Event& operator += (const event_type& _ev)
   {
    assert(std::find(m_Events.begin(), m_Events.end(), _ev) == m_Events.end());
    m_Events.push_back(_ev);
    return *this;
   }; // eo +=

   Event& operator -= (const event_type& _ev)
   {
    EventVector_cit cit(std::find(m_Events.begin(), m_Events.end(), _ev));
    assert(cit != m_Events.end());
    m_Events.erase(cit);
    return *this;
   }; // eo -=
  }; // eo class Event

And during compilation:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(41): error C2451: conditional expression of type 'void' is illegal
1>          Expressions of type void cannot be converted to other types

Now, I understand this is because of what is being stored in the vector and the operator ==. Is there another way to store std::function in an STL container? Do I need to wrap it up in something else?

解决方案

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.

这篇关于std ::函数的std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆