3

私は次のものを持っています:

  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

そしてコンパイル中:

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

これは、 vector と operator に格納されているものが原因であることを理解しています==std::functionSTLコンテナに保存する別の方法はありますか? 他のものでまとめる必要がありますか?

4

1 に答える 1

0

boost::functionを使用しない場合は、ベクターに格納できますstd::find。これが必要なようなので、関数を独自のクラスに同等にラップするのがおそらく最善です。

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

これには、 を適切な方法で維持する必要があることに注意してくださいid_(たとえば、2 つEventFunの異なる は異なる を持ちますid_)。

別の可能性はboost::function、クライアントが記憶し、削除時に特定の機能を識別するために使用するタグを使用して s を保存することです。

于 2010-12-13T15:57:02.530 に答える