See the following code:
std::function<int(int)> makeFibonacci() {
std::function<int(int)> fibonacci = [&fibonacci](int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 1;
}
return fibonacci(n-1) + fibonacci(n-2);
};
return fibonacci;
};
int main() {
std::function<int(int)> fibonacci = makeFibonacci();
std::cout << fibonacci(6) << std::endl;
return 0;
}
When I run this, the number 8
is output as expected. However when I change the captured &fibonacci
to just fibonacci
for a by-copy capture, the program actually segfaults on the first line of main
where it runs makeFibonacci
.
If fibonacci
(line 2) is a local of the makeFibonacci
function, and therefore goes out of scope when the function exits, how can it be captured by reference and used recursively? Also, why does the program segfault when I capture the lambda by copy?