How is it possible that this example works? It prints 6:
#include <iostream> #include <functional> using namespace std; void scopeIt(std::function<int()> &fun) { int val = 6; fun = [=](){return val;}; //<-- this } int main() { std::function<int()> fun; scopeIt(fun); cout << fun(); return 0; } Where is the value 6 stored after scopeIt is done being called? If I replace the [=] with a [&], it prints 0 instead of 6.