I used to write lambdas like this:
std::vector<....> data;
//here we're populating data
mylambda=[data{std::move(data)}](...){...}
...
std::function<....> myfunction= mylambda;
...
return myFunction
At first, this seemed like an ok solution, but recently I found that, after being called, std::function calls reset
function, which re-initializes the parameters of the lambda function. This seems like something that could potentially cause problems (since when reset is called, data
variable is already out of context), but so far I haven't had any issues with this.
Is initializing lambda parameters by moving an object which does out of context a good idea?