logoalt Hacker News

porridgeraisinlast Sunday at 7:00 PM2 repliesview on HN

If the lambda captures some value, and also outlives the current scope, then that captured value has to necessarily be heap allocated.


Replies

noselasdlast Sunday at 8:05 PM

No, (in C++) the lambda can capture the the variable by value, and the lambda itself can be passed around by value. If you capture a variable by reference or pointer that your lambda outlives, your code got a serious bug.

show 1 reply
do_not_redeemlast Sunday at 7:09 PM

That would be a bug, so just... don't do that?

If you return a pointer to a local variable that outlives the scope, the pointer would be dangling. Does that mean we should ban pointers?

If you close over a pointer to a local variable that outlives the scope, the closure would be dangling. Does that mean we should ban closures?