title: Callback to member function using libevent
body: <p>I am trying to pass a member function to <a href="http://www.libevent.org" rel="nofollow">libevent</a> which should be treated as a callback. </p>
  
  <pre><code>#include &lt;event.h&gt;
  
  class A
  {
      public:
          void eventcb(evutil_socket_t fd, short events, void *ctx) { }
  };
  
  
  static void global_eventcb(evutil_socket_t fd, short events, void *ctx) { }
  
  typedef void (A::*mthd)(evutil_socket_t, short, void*);
  
  int main(void)
  {
      struct event_base *evbase = event_base_new();
  
      mthd eventcb = &amp;A::eventcb;
      A *instance = new A;
      (instance-&gt;*eventcb)(NULL, 0, NULL);
  
      struct event *timer1 = evtimer_new(evbase, global_eventcb, NULL);
      struct event *timer2 = evtimer_new(evbase, (instance-&gt;*eventcb), NULL);
  
      return 0;
  }   
  </code></pre>
  
  <p>I can successfully create a method pointer to <em>eventcb</em> in class A and call it on an instance of A (row 20).</p>
  
  <p>Also, passing a global function (as one would do in C) on row 22 also works fine.</p>
  
  <p>However, on row 23, I attempt to pass my method pointer to <em>libevent</em>, and when I compile this I get the following error (using the <em>clang</em> compiler)</p>
  
  <pre><code>example.cpp:23:25: error: no matching function for call to 'event_new'
          struct event *timer2 = evtimer_new(evbase, (instance-&gt;*eventcb), NULL);
                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  In file included from example.cpp:1:
  In file included from /usr/local/include/event.h:71:
  /usr/local/include/event2/event.h:749:40: note: instantiated from:
  #define evtimer_new(b, cb, arg)        event_new((b), -1, 0, (cb), (arg))
                                         ^~~~~~~~~
  /usr/local/include/event2/event.h:833:15: note: candidate function not viable: no know conversion from '&lt;bound member function type&gt;' to 'event_callback_fn'
        (aka 'void (*)(int, short, void *)') for 4th argument
  struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
                ^
  1 error generated.
  </code></pre>
  
  <p>What am I doing wrong?</p>