returning struct, destructor

Eugene Wissner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 21 03:45:18 PST 2016


Consider we have a function that returns a struct. So for example:

import std.stdio;

struct A {
     ~this() {
         writeln("Destruct");
     }
}

A myFunc() {
     auto a = A(), b = A();
     if (false) {
         return a;
     }
     return b;
}

void main() {
     myFunc();
}

This prints 3 times "Destruct" with dmd 0.072.1. If I remove the 
if block, it prints "Destruct" only 2 times - the behavior I'm 
expecting. Why?
Thx



More information about the Digitalmars-d-learn mailing list