From my experience it seems that either:
- A lambda expression created inside a function call is destroyed just after the invocation
- Calling a function that expects a
std::functioncreates a temporary object (std::function) out of the lambda, and that object is destroyed after invocation
This behavior can be observed with the following snippet of code:
const function<void()>* pointer;
void a(const function<void()> & f)
{
pointer = &f;
}
void b()
{
(*pointer)();
}
int main()
{
int value = 1;
std::cout << &value << std::endl;
// 1: this works
function<void()> f = [&] () { std::cout << &value << std::endl; };
a(f);
// 2: this doesn't
a([&] () { std::cout << &value << std::endl; });
/* modify the stack*/
char data[1024];
for (int i = 0; i < 1024; i++)
data[i] = i % 4;
b();
return 0;
}
What exactly s actually happening in the second case?
Is there a correct way to call a() without creating an explicit std::function object?
Edit:: This both versions (1 and 2) compile just right but result in different outputs:
Version 1:
0x7fffa70148c8
0x7fffa70148c8
Version 2:
0x7fffa70148c8
0