scope(exit) considered harmful

Walter Bright newshound1 at digitalmars.com
Sun Nov 8 10:39:58 PST 2009


Bill Baxter wrote:
> Well, in C++ you'd probably wrap up myRegionAllocator in a struct and
> put the .destroy call in its destructor.  For use you'd put an
> instance of the struct on the stack.  But scope(exit) is much more
> convenient than having to put all your cleanup code into a struct.

Everything you can do with scope, you can do with C++ RAII objects. But 
that's like saying everything you can do with C++ virtual functions you 
can do in C using tables of function pointers.

Also, everything you can do with scope you can do with some combination 
of try/catch/finally. In fact, this is demonstrably true because the D 
front end rewrites all the scope statements into try/catch/finally. But 
like doing OOP in C with function pointer tables, who would want to?



More information about the Digitalmars-d mailing list