C ++ 11 lambda high-order function wrapper recursive error

#include <iostream>
#include <functional>
using namespace std;

function<int(int)> wrapper(function<void(int)> f)
{
    auto ff = [&](int a) {
        cout << "in wrapper " << a << endl;
        f(a);
        return 1;
    };
    return ff;
}

int main()
{
    auto fa = wrapper([](int a){
        cout << "in fa " << a << endl;
    });
    fa(999);
    wrapper([&fa](int b){
        cout << "in anon " << b << endl;
        fa(998);
    })(997);
}

      

The above code will print

in wrapper 999
in fa 999
in wrapper 997
in anon 997
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
..........

      

before segmentation fault.

I am writing the same code in javascript

function  wrapper(f)
{
    var ff = function(a) {
        console.log("in wrapper %s", a);
        f(a);
        return 1;
    };
    return ff;
}

(function ()
{
    var fa = wrapper(function(a){
        console.log("in fa %s", a);
    });
    fa(999);
    wrapper(function(b){
        console.log("in anon %s", b);
        fa(998);
    })(997);
})();

      

It will print

in wrapper 999
in fa 999
in wrapper 997
in anon 997
in wrapper 998
in fa 998

      

What's with the C ++ lambda code? I tried the compiler g ++ - 5.0 and g ++ 4.9. It all ended with this error.

+3


source to share


1 answer


ff

commits f

by reference, but f

is local to wrapper

. When you return ff

, that link becomes dangling and undefined behavior is triggered when called ff

.



+4


source







All Articles