DIP44: scope(class) and scope(struct)

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Aug 23 22:06:18 PDT 2013


On Sat, Aug 24, 2013 at 06:19:25AM +0200, Meta wrote:
> "With this extension to scope guards, class and struct destructors
> will practically not be needed anymore, since scope(this) will take
> care of cleaning up everything."
> 
> I can't think of an example off the top of my head, but is it really
> okay to conflate destruction due to an error during construction,
> and destruction over the regular course of a struct's usage? What if
> one instance requires different code from the other? Maybe require
> that scope(this) statements *only* be run if there is an error
> during construction, while just the destructor will be run normally?

If a particular cleanup operation only applies to a ctor failure, you
could just use scope(failure) in the ctor, and put the different cleanup
code in the dtor.

scope(this) is really meant to encapsulate the usual (I think!) case
where the same cleanup code applies in both cases.

Maybe I was a bit too strong to say dtors won't be needed anymore, but
if you leave dtors in, then they can still handle this case. :)


T

-- 
May you live all the days of your life. -- Jonathan Swift


More information about the Digitalmars-d mailing list