Idomatic way to guarantee to run destructor?

Robert M. Münch robert.muench at saphirion.com
Thu Apr 30 16:55:36 UTC 2020


For ressource management I mostly use this pattern, to ensure the 
destructor is run:

void myfunc(){
  MyClass X = new MyClass(); scope(exit) X.destroy;
}

I somewhere read, this would work too:

void myfunc(){
  auto MyClass X = new MyClass();
}

What does this "auto" does here? Wouldn't

void myfunc(){
  auto X = new MyClass();
}

be sufficient? And would this construct guarantee that the destructor 
is run? And if, why does "auto" has this effect, while just using "new" 
doesn't guarantee to run the destructor?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list