gpt4 book ai didi

c++ - 编写 lambda 函数 vector 时出现段错误

转载 作者:行者123 更新时间:2023-11-28 01:41:34 26 4
gpt4 key购买 nike

我正在尝试创建一个 compose_all lambda,它接受一个函数 vector ,并返回一个函数,该函数是 vector 中所有函数的组合:

#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>

using std::cout;
using std::endl;
using std::function;
using std::vector;

int main() {
vector<function<int(int)>> functions = {
[](int x) { return 2 * x; },
[](int x) { return x * x; },
[](int x) { return -x; },
};

function<function<int(int)>(
vector<function<int(int)>>::iterator,
vector<function<int(int)>>::iterator,
function<int(int)>)> compose_all;
compose_all = [&](vector<function<int(int)>>::iterator f_begin,
vector<function<int(int)>>::iterator f_end,
function<int(int)> f) -> function<int(int)> {
for (; f_begin < f_end; ++f_begin) {
f = [&](int x) { return (*f_begin)(f(x)); };
}
return f;
};

auto composition = compose_all(functions.begin(),
functions.end(),
[](int x) { return x; });

for (int i = 0; i < 10; ++i) {
cout << composition(i) << endl;
}
return 0;
}

虽然编译正常,但会出现段错误:

$ clang++ -std=c++11 -g composition.cpp && ./a.out 
Segmentation fault (core dumped)

段错误的原因是什么,修复方法是什么?

使用 print 语句和 GDB 进行调试的注意事项:

  • compose_all 正确接收迭代器
  • 段错误发生在行 f = [&](int x) { return (*f_begin)(f(x)); };
  • 单独取消引用 f_begin 会产生正确的结果(它在 vector 中调用正确的 lambda)

最佳答案

创建的每个 lambda

f = [&](int x) { return (*f_begin)(f(x)); };

通过引用捕获 ff_begin,其中两者都是您存储在 compose_all 中的 lambda 的主体。

这些函数中的最后一个在调用时由 compose_all 包含的 lambda 的主体返回,然后分配给 composition。但是由于 compose_all 的 lambda 主体已经退出,ff_begin 的生命周期已经结束,并且调用 composition 是未定义的行为。

此外,您并不真的希望 f 调用自身,这只会让您无限递归。您希望 f 从其当前值(即初始值,或从上次分配 f)。

你需要这样的东西:

const auto& g = *f_begin;
f = [=](int x) { return g(f(x)); };

(或者在 C++14 或更高版本中,可以这样写:)

f = [f, g=*f_begin](int x) { return g(f(x)); };

关于c++ - 编写 lambda 函数 vector 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com