Destructor call

n0fun no at mail.com
Tue Apr 10 18:34:54 UTC 2018


import std.stdio;

struct S(alias n) {
     this(int) {
         throw new Exception("Exception");
     }
     ~this() {
         writeln("destructor " ~ n);
     }
}

void main() {
     writeln("--- 1 ---");
     try {
         auto s = S!"1"(0);
     } catch (Exception) {}
     writeln("--- 2 ---");
     try {
         auto s = new S!"2"(0);
     } catch (Exception) {}
}

Output:
--- 1 ---
--- 2 ---
destructor 2

Why the destructor is called in the second case and why not in 
the first?
How to design structs with such different behavior?



More information about the Digitalmars-d-learn mailing list