#include <functional>
std::function<int()> dowork()
{
int answer = 42;
auto lambda = [answer] () { return answer; };
// can do what we want
answer = 666;
return lambda;
}
int main()
{
auto ll = dowork();
return ll(); // 42
}
I2luY2x1ZGUgPGZ1bmN0aW9uYWw+CgpzdGQ6OmZ1bmN0aW9uPGludCgpPiBkb3dvcmsoKQp7CiAgICBpbnQgYW5zd2VyID0gNDI7CiAgICBhdXRvIGxhbWJkYSA9IFthbnN3ZXJdICgpIHsgcmV0dXJuIGFuc3dlcjsgfTsKCiAgICAvLyBjYW4gZG8gd2hhdCB3ZSB3YW50CiAgICBhbnN3ZXIgPSA2NjY7CiAgICByZXR1cm4gbGFtYmRhOwp9CgppbnQgbWFpbigpCnsKICAgIGF1dG8gbGwgPSBkb3dvcmsoKTsKICAgIHJldHVybiBsbCgpOyAvLyA0Mgp9Cg==