title: Vector of std::function<>
body: <p>Say want to store the following:</p>
  
  <pre><code>typedef std::function&lt;void(int)&gt; MyFunctionDecl;
  </code></pre>
  
  <p>..in a collection:</p>
  
  <pre><code>typedef std::vector&lt;MyFunctionDecl&gt; FunctionVector;
  FunctionVector v;
  </code></pre>
  
  <p>This is possible, but if I want to <em>find</em> something using <code>std::find</code>:</p>
  
  <pre><code>FunctionVector::const_iterator cit = std::find(v.begin(), v.end(), myFunctionDecl);
  </code></pre>
  
  <p>.. we get an error due to the <code>==</code> operator.</p>
  
  <p>As has been suggested to me in a previous question concerning this, this can be gotten around by encapsulating the function declaration within another class, which provides a <code>==</code> operator:</p>
  
  <pre><code>class Wrapper
  {
  private:
      MyFunctionDecl m_Func;
  
  public:
      // ctor omitted for brevity
  
      bool operator == (const Wrapper&amp; _rhs)
      {
           // are they equal?
      }; // eo ==
  };  // eo class Wrapper
  </code></pre>
  
  <p>So what I want to do is somehow generate a hash for "MyFunctionDecl" so that I can properly implement the <code>==</code> operator.  I could have some kind of unique identifier, and ask the caller to provide a unique identifier for the delegate, but this seems a bit of a pain and is error prone.</p>
  
  <p>Is there a way that I can do this?  So that the same functions will return the same ID for comparative purposes?  So far, the only way around it is to dump the notion of using <code>std::function</code> and go back to using fast delegates which support comparisons.  But then I lose the ability to use lambdas.</p>
  
  <p>Any help appreciated!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Given the answer below, this is what I have come up with...  any caveats I might have missed?  I'm in the process of putting it through it's paces now:</p>
  
  <pre><code>class MORSE_API Event : boost::noncopyable
  {
  public:
      typedef std::function&lt;void(const EventArgs&amp;)&gt; DelegateType;
      typedef boost::shared_ptr&lt;DelegateType&gt; DelegateDecl;
  
  private:
      typedef std::set&lt;DelegateDecl&gt; DelegateSet;
      typedef DelegateSet::const_iterator DelegateSet_cit;
      DelegateSet m_Delegates;
  
  public:
      Event()
      {
      };  // eo ctor
  
  
      Event(Event&amp;&amp; _rhs) : m_Delegates(std::move(_rhs.m_Delegates))
      {
      };  // eo mtor
  
      ~Event()
      {
      };  // eo dtor
  
      // methods
      void invoke(const EventArgs&amp; _args)
      {
          std::for_each(m_Delegates.begin(),
                        m_Delegates.end(),
                        [&amp;_args](const DelegateDecl&amp; _decl) { (*_decl)(_args); });
      };  // eo invoke
  
      DelegateDecl addListener(DelegateType f)
      {
          DelegateDecl ret(new DelegateType(f));
          m_Delegates.insert(ret);
          return ret;
      };  // eo addListener
  
      void removeListener(const DelegateDecl _decl)
      {
          DelegateSet_cit cit(m_Delegates.find(_decl));
          if(cit != m_Delegates.end())
              m_Delegates.erase(cit);
      };  // eo removeListener
  
  };  // eo class Event
  </code></pre>