Segfault when not specifying return type of lambda function
A lambda without trailing return type as in:
[&](){return str;};
Is equivalent to:
[&]()->auto{return str;};
So this lambda returns a copy of str.
Calling the std::function
object will result in this equivalent code:
const string& std_function_call_operator(){
// functor = [&]->auto{return str;};
return functor();
}
When this function is called, str
is copied inside a temporary, the reference is bound to this temporary and then the temporary is destroyed. So you get the famous dangling reference. This is a very classical scenario.