We can define recursive lambda function like
std::function<void(int)> fun = [&fun](int a) { if (a) fun(a - 1); };
fun(10);
std::function<void(int)> fun = [fun](int a) { if (a) fun(a - 1); };
fun(10);
Capture by value is evaluated as part of evaluating the lambda expression. At that time, fun
is still uninitialised, because you're still evaluating its initialiser. Only after that is fun
initialised, but by that time the copy has already happened.
The net effect is that the lambda function object stored inside fun
has a data member named fun
which is a copy of an uninitalised std::function
— Undefined Behaviour.