Quote:Original post by Rydinare
If you need to replicate a finally block as in Java, there's two approaches which can be useful in different circumstances.
1) Using ScopeGuard, which is part of the Loki library.
I've seen it, it's ok. But it suffers from being too verbose, and it limites you to extremely simple operations. For example:
void Decrement(int& x) { --x;}void UseResource(int refCount) { ++refCount; ScopeGuard guard = MakeGuard(Decrement, refCount); ...}
Instead of this:
void UseResource(int refCount) { ++refCount; Finally { --refCount; } ...}
Can you see how theirs doesn't scale well with code complexity? You'll have to define a dummy function for every little operation you want to do. Otherwise you're stuck with a limited set of one-liners.
But yes, I respect ScopeGuard and if I didn't have a choice it's the one I'd probably use.
Quote:2) You can replectate try-finally in C++ using the following idiom:
*** Source Snippet Removed ***
Unfortunately, you still need to duplicate "finally_code" at every return statement. It's a serious penalty for any sizable project.
Quote:So, personally, I would either avoid the asserts when an exception is thrown...
That's possible with the standard function uncaught_exception() I mentioned.
I'll post back when I got this thing working, probably later today (I've got other stuff to do too :) ).