0

There is a callback function type in libevent used by event_new().

typedef void (*event_callback_fn)(evutil_socket_t, short, void *);

I want use lambda with event_callback_fn.

If I use

[](evutil_socket_t fd, short flags, void * _param){}

everything is OK.
But if I use the lambda capture list

[&](evutil_socket_t fd, short flags, void * _param){} 

event_new() will not be compiled.

DinoStray
  • 696
  • 1
  • 6
  • 20
  • 2
    Only a capture-less lambda may be converted to a pointer to functions. If you need captures then you need to solve it some other way. – Some programmer dude Nov 22 '18 at 09:40
  • Short answer is no. You should understand what [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming))s and [callback](https://en.wikipedia.org/wiki/Callback_(computer_programming))s are. However, you could pass the closure as the `void *` client data – Basile Starynkevitch Nov 22 '18 at 09:40
  • @ Basile Starynkevitch, so event_new(_base, -1, EV_TIMEOUT, [](evutil_socket_t fd, short flags, void * _param){ }, [](){}); like this? still compile error – DinoStray Nov 22 '18 at 09:49

1 Answers1

4

The type alias

void (*event_callback_fn)(evutil_socket_t, short, void *);

is a function pointer. Lambdas can automatically convert to function pointers, when they don't capture anything. As soon as you define a closure (stateful lambda), you can't pass it as an argument of type event_callback_fn.

lubgr
  • 37,368
  • 3
  • 66
  • 117