Hacker News new | ask | show | jobs
by sandeatr 3421 days ago
C++ does not need finally, so it would not make much sense to add it. RAII is superior and is what is used instead.

Finally would likely just encourage people to write really awful C++ code.

2 comments

So are you saying that C++ has no plans to implement finally?

(also I totally disagree that RAII is superior to finally. Finally, being lexically scoped, is far simpler, far shorter and far easier to understand. https://codecraft.co/2013/10/31/why-we-need-try-finally-not-... )

That article does not convince me, scope guard + lambda appears to work just as well as finally, and is actually shorter(see tianyuzhu's comment)

I do not know if anyone has proposed it for C++, but I would be surprised if it passed given that it doesn't accomplish much.

RAII is not superior both ways have their own drawbacks. You can forget to write finally and it may produce duplicated code. On the other hand RAII requires you to create new type just for exception safety (scope guard - which you may also forget to use). I prefer RAII but there are situations where finally leads to more readable code.