简体   繁体   English

意外的std :: bad_function_call递归

[英]unexpected std::bad_function_call in recursion

I have written the following code, which plays with functions of type function<int(int)> . 我已经编写了以下代码,可与function<int(int)>类型的function<int(int)>一起使用。 The functions compose , print , inc and guarded are helpers which combine other functions or produce some external effect. 函数composeprintincguarded是帮助程序,可以组合其他功能或产生一些外部效果。 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. 但是,当调用recursion()(0) ,第二次到达go时引发了std::bad_function_call异常,但我不知道为什么。 Is there any dangling reference or empty std::function ? 是否有任何悬挂的引用或空的std::function Moreover, eta-expanding go works: 此外,eta扩展go工作:

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 . 在您的原始代码中,在go初始化期间调用了recursion() recursion itself tries to use the value of go , but at that point go has not been initialized yet, causing the problem. recursion本身尝试使用go的值,但是在这一点上, go尚未初始化,从而导致了问题。

In the alternative code, the initialization of go only assigns a lambda to go without calling recursion . 在替代代码中, go的初始化仅将lambda分配给go而没有调用recursion When recursion is called later, go will already have been initialized. 稍后调用recursiongo已经被初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导致std :: bad_function_call的原因是什么? - What causes std::bad_function_call? stst :: bad_function_call的callstack - Callstack for std::bad_function_call 从外部函数构造 std::function 给出 std::bad_function_call - Constructing std::function from extern functions gives std::bad_function_call c++ std::map 比较函数导致运行时“bad_function_call” - c++ std::map compare function leads to runtime "bad_function_call" std :: bad_function_call在visual studio中调用std :: swap时 - std::bad_function_call when calling std::swap in visual studio std :: bad_function_call自调用递归lambda但没有提示 - std::bad_function_call of self-calling recursive lambda but no hints 为什么我的线程池有时会抛出 `std::bad_function_call` 或 `double free or corruption (!prev)` - Why does my thread pool sometimes throw `std::bad_function_call` or `double free or corruption (!prev)` 将 avx 变量传递给 std::function 时引发 bad_function_call 和分段错误 - bad_function_call thrown and segmentation fault caused when passing avx variables to std::function 运行 dpcpp 代码时终止代码为 std::bad_function_call - Terminating code as std::bad_function_call when running the dpcpp code 使用Lambda的C ++回调失败并显示bad_function_call - C++ callback with lambda fails with bad_function_call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM