Re:In Defense of C++
by retrogradeorbit on Tuesday February 07, 2017 @01:54AM
(#13587643)
AFAIK C++ still doesn't have a finally construct for its exceptions. Are there any plans to introduce it? Is so, when does it land?
[ Parent
| on HN ]
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.
Re:In Defense of C++
by retrogradeorbit on Tuesday February 07, 2017 @02:57AM
(#13587862)
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.