struct MyStruct {
    string name;
    ~this() {
        import std.stdio;
        writeln("destroying ", name);
    }
}

void main() {
    auto a = MyStruct("a"), b = MyStruct("b");
    {
        auto e = MyStruct("scoped e");
    }
    auto c = MyStruct("c");
    MyStruct[3] f = [MyStruct("1"), MyStruct("2"), MyStruct("3")];
    return;
    auto d = MyStruct("d");
}

With current DMD, this outputs:

destroying scoped e
destroying 3
destroying 2
destroying 1
destroying c
destroying b
destroying a

That is, destruction happens in reverse lexical order of declaration, and arrays are destroyed from back to front.

Is this behaviour guaranteed?

Reply via email to