0

I have a function that accepts a function pointer like so:

void invoke(void (*fnCallback)(int data)) { ... }

I also have a class method from which I would like to call this function, and pass the callback as a lambda:

void Class::testInvoke() {
  auto f = [](int data) -> void { data = data + 1; };
  invoke(f);
}

This works, but if I try capturing with the lambda, the code does not compile:

void Class::testInvoke() {
  int value = 1;
  auto f = [=](int data) -> void { data = data + value; };
  invoke(f);
}

In the second case, if I comment out the invoke(f) line, the code compiles. What am I doing wrong? How do I need to change the signature of invoke so it accepts captured lambdas?

gsamaras
  • 69,751
  • 39
  • 173
  • 279
AweSIM
  • 1,586
  • 3
  • 17
  • 32

0 Answers0