3 ms·
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
by sandeatr 10y 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.
- retrogradeorbit 10y agoSo 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-just-raii/ https://codecraft.co/2013/10/31/why-we-need-try-finally-not-... )
- sandeatr 10y agoThat 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.
- oselhn 10y agoRAII 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.