Your program has undefined behavior. Your lambda captures a local variable by address. Thus, the program can do anything it want. [insert old adage about undefined behavior erasing hard drives, launching nethack, etc.]
On Wed, Aug 13, 2014 at 8:40 AM, Jack Wu <[email protected]> wrote: > Hello: > > Here is the c++ lambda code I test: > #include <iostream> > #include <functional> > using namespace std; > > function<int(int)> doSome() > { > int x = 10; > auto f = [&x](int y) { > x = x + y; > return x; //since x is a local variable, it will be free when leaving > doSome > }; > return f; > //return [x](int y) { return x+y; }; > } > > int main() > { > function<int(int)> func = doSome(); > int first_call = func(10); > int second_call = func(10); > cout << first_call << '\n'; > cout << second_call << '\n'; > system("pause"); > } > > As you can see, if I run the code on my host pc using gcc or clang > compiler, the output will occur error like this: > > > <https://lh3.googleusercontent.com/-Ek2GE7qwu54/U-uDuhBxHvI/AAAAAAAAAWE/MuwCaHrByY0/s1600/test.png> > > > > However, if I compile to Asm.js. It works and the results show that it > behaves like a closure function. > > > <https://lh3.googleusercontent.com/-Qgynh9Ukn94/U-uGBetO-0I/AAAAAAAAAWQ/k9qN5paPWKI/s1600/22.png> > > > Is this a normal situation or is there any potential danger if I were to > use lambda function in c++ then compile to Asm.js ? > > Thank you. > > > > > > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Chad Austin Technical Director, IMVU http://engineering.imvu.com <http://www.imvu.com/members/Chad/> http://chadaustin.me -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
