I'm having a trouble finding a definitive answer if the following code is correct in both cases, with non-capturing lambda and with call of a static function. I'm pretty sure the latter is fine, what I'm concerned about is the lifetime of a lambda when "defined" inside another block/function call, etc. The code seems to work fine but I'd still prefer to know why... Does the non-capturing lambda end up defined as some kind of anonymous (hidden) global symbol so it's basically the same as that static function?
#include <cstdio>
class Listener {
public:
void event() { printf("event() called\n"); }
static void forwardEvent(void* userArg) { static_cast<Listener*>(userArg)->event(); }
};
class Producer {
using Listener = void(void* arg);
Listener* listener_;
void* userArg_;
public:
void attachListener(Listener* listener, void* userArg) {
listener_ = listener;
userArg_ = userArg;
}
void issueEvent() { listener_(userArg_); }
};
int main() {
Listener listener;
Producer producer;
{ // different scope, in case that makes a difference...
producer.attachListener(Listener::forwardEvent, &listener);
}
producer.issueEvent();
{ // different scope, in case that makes a difference...
producer.attachListener([](void* userArg) { static_cast<Listener*>(userArg)->event(); }, &listener);
}
producer.issueEvent();
}