简体   繁体   中英

unexpected std::bad_function_call in recursion

I have written the following code, which plays with functions of type function<int(int)> . The functions compose , print , inc and guarded are helpers which combine other functions or produce some external effect. Then I use them to build my programs:

/* start of the program */
function<int(int)> recursion();

function<int(int)> go =
  compose(guarded(10, recursion()), compose(inc, print("go")));

function<int(int)> recursion() {
  return compose(go, print("recursion"));
}

However, when calling recursion()(0) , an exception std::bad_function_call was thrown when go was reached the second time but I don't see why. Is there any dangling reference or empty std::function ? Moreover, eta-expanding go works:

function<int(int)> go = [](int n) -> int {
  return compose(guarded(10, recursion()), compose(inc, print("go")))(n);
};

What's wrong with the original code? Why does the alternative one work?

Full code:

#include <string>
#include <iostream>
#include <functional>

using namespace std;

/* helper functions, some combinators */

//composing two functions, f1 . f2
function<int(int)> compose(const function<int(int)>& f1, const function<int(int)>& f2) {
  return [f1,f2](int n) -> int {
    return f1(f2(n));
  };
}

function<int(int)> print(const string& msg) {
  return [msg](int n) -> int {
    cout << "print: " << msg << endl;
    return n;
  };
}

function<int(int)> inc = [](int n) -> int {
  cout << "in inc lambda: " << n << endl;
  return n+1;
};

//calls the given function `f` only when `n` is less then `m`
function<int(int)> guarded(int m, function<int(int)> f) {
  auto g = [m,f](int n) -> int { return n<m? f(n) : m; };
  return compose(g, print("guarded"));
}

/* start of the program */
function<int(int)> recursion();

function<int(int)> go =
  compose(guarded(10, recursion()), compose(inc, print("go")));

function<int(int)> recursion() {
  return compose(go, print("recursion"));
}

int main() {
  try {
    recursion()(0);
  } catch (bad_function_call e) {
    cout << "bad_function_call: " << e.what() << endl;
  }
  return 0;
}

In your original code, recursion() is called during the initialization of go . recursion itself tries to use the value of go , but at that point go has not been initialized yet, causing the problem.

In the alternative code, the initialization of go only assigns a lambda to go without calling recursion . When recursion is called later, go will already have been initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM