Consider this:
#include <iostream> #include <functional> std::function<void()> task; int x = 42; struct Foo { int& x; void bar() { task = [=]() { std::cout << x << '\n'; }; } }; int main() { { Foo f{x}; f.bar(); } task(); }
My instinct was that, since the actual referent still exists when the task is completed, we get a linked link during lambda, and everything is fine.
However, on my GCC 4.8.5 (CentOS 7), I see some behavior (in a more complex program) that assumes that this is instead of UB, because f and the fx link itself died, Is this correct?
c ++ lambda c ++ 11
Lightness races in orbit
source share