Naryc
Naryc

Reputation: 211

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                                                                                                                                                     
..........

until Segmentation fault.

I write 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 g++-5.0 and g++4.9 compiler. All ended up with this error.

Upvotes: 3

Views: 144

Answers (1)

Quentin
Quentin

Reputation: 63124

ff captures f by reference, but f is local to wrapper. When you return ff, that reference becomes dangling, and undefined behaviour is triggered upon calling ff.

Upvotes: 4

Related Questions