From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-04-09 02:07:31
Monday, April 9, 2007, 4:36:36 AM, you wrote:
> Well, I'm very late to this thread of discussion, but I'd like to add my
> Why would we go through so much complexity? Remember the simple and basic
> principle of RAII? In situations like this, you just make a simple wrapper
> of the FILE*, with an implementation similar to that of a smart pointer such
> as the auto_ptr, and let's name it auto_cfile, with a "FILE*&()" operator
> member function to expose the FILE* it protects:
> auto_cfile f(std::fopen("/etc/passwd","r"));
> ..... (uses of f as if it were a FILE*)
> and the FILE* is automatically closed by auto_cfile's dtor when it goes out
> of scope.
> No need of a "try ... catch" block at all, much less of "finally," which is
> an unnecessary addition in C#, IMHO.
In a case as simple as FILE* I may agree with you. But sometimes I
have to do something more complicated than fclose, and this something
is not associated with any handle like FILE*. For example, I can
express the code to be executed inevitably, even if an exception
occurs at some point, as a scope guard.
// some code here...
// This code is executed even if the function below
// throws. This function may be out of this module's
// scope, so I generally don't know (and don't care)
// wether it does throw or not, and if it does, what
// it does throw.
Another good use case for scope guards is making rollbackable code.
But I see, this implementation doesn't support that.
-- Best regards, Andrey mailto:andysem_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk