c++ can't init a value capture lambda by std::function if it's a recursive func

Viewed 76

I'm trying t write a recursive lambda warped by std::function like this:

function<int(int)> fib {[&](int n) {
  assert( n >= 0 );
  if( n < 2 ) return val;
  else
    return fib(n-1) + fib(n-2);
}};

It's worked, but if I change the capture method from '&' to '=', I found this code can be compiled but will core(segmentation fault) at fib init.

That's why?

Thanks in advance :)

0 Answers
Related