Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>"which actually shows the insane amount of hacking required to make a lambda function call itself"

I doesn't require any hack at all, it doesn't even require to capture the variable. You just need to be sure to make it persist and be accessible in the scope of the lambda.

For example:

http://ideone.com/6hwVGy

That said, please don't do it in production code. std::function is very inefficient when it is being used in a recursive way



Why is it inefficient with recursion? I know that at least non-recursive usage of std::function is generally well-optimized by most compilers.


When you wrap a lambda into a std::function variable, it involves a call to a virtual function which add overheads (often unnoticed unless you repeat the call several times), so a regular recursive functor would be even faster.

The subject was discussed in the following threads:

http://stackoverflow.com/questions/2067988/recursive-lambda-...

http://stackoverflow.com/questions/17066667/overhead-of-recu...


The lambda function was an event handler, that needed to add itself to the event loop under certain conditions (retry).

Its parent scope is not persistent.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: