Given the following function that returns a lambda
auto foo(int y) {
return [=](int x) { return x + y; };
}
GCC and Clang produce the following assembly
foo(int):
mov eax, edi
ret
which as far as I can tell is equivalent to this
int bar(int n) {
return n;
}
Yet bar
seems to function as expected.
auto fn = foo(2);
std::cout << fn(3); //prints 5