scope(~this)

Inquie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 12 14:38:44 PDT 2017


Is there any easy way to create a scope for termination of the 
object?

I have a template method that takes a type and allocates and 
deallocates based on that type.

class bar
{
    void foo(T)()
    {
       T x;
       alloc(x);
       scope(~this) dealloc(x); // hypothetical that wraps the 
statement in a lambda and deallocates in the destructor

       ... x must stay allocated until class instance 
termination(has to do with COM, can't release it in foo)
    }

}

Now, x cannot be a field because T is unknown(i suppose I could 
use object, void*, etc, but...).



I realize there are ways to work around this. I will create 
lambdas that release them since that is easiest. I could return x 
but that creates a mess.

Hoping that either something like this exists or could be a 
feature enhancement.





More information about the Digitalmars-d-learn mailing list