logoalt Hacker News

LexiMaxtoday at 5:49 AM4 repliesview on HN

A long overdue feature.

Though I do wonder what the chances are that the C subset of C++ will ever add this feature. I use my own homespun "scope exit" which runs a lambda in a destructor quite a bit, but every time I use it I wish I could just "defer" instead.


Replies

pjmlptoday at 8:57 AM

Never, you can already do this with RAII, and naturally it would be yet another thing to complain about C++ adding features.

Then again, if someone is willing to push it through WG21 no matter what, maybe.

show 1 reply
anilakartoday at 8:26 AM

Various macro tricks have existed for a long time but nobody has been able to wrap the return statement yet. The lack of RAII-style automatic cleanups was one of the root causes for the legendary goto fail;[1] bug.

[1] https://gotofail.com/

show 1 reply
mwkaufmatoday at 2:03 PM

Just hope those lambdas aren't throwing exceptions ;)

surajrmaltoday at 7:58 AM

In many cases that's preferred as you want the ability to cancel the deferred lambda.