When a struct is passed to a function as argument, it is first copied and at the end destructed. But in the following code it is not copied, yet still destructed?

module main;

import std.stdio;

struct C
{
        A[] _objs;

        this(A[] objs...)
        {
                writeln(`  C this()`);
                _objs = objs;

                // A.this(this) is not called
                // yet A.~this IS called
        }
}

struct B
{
        A sup;
        alias sup this;

        this(A a)
        {
                writeln(count, ` B this()`);
                sup = a;
        }
}

struct A
{       
        static int count;

        this(int n)
        {
                count++;
                writeln(count, ` A this()`);
        }

        this(this)
        {
                count++;
                writeln(count, ` A this(this)`);
        }

        ~this()
        {
                count--;
                writeln(count, ` A ~this()`);
        }
}

void main()
{
        A a = A(1);
        writeln(a.count == 1);

        B b = B(a);
        writeln(a.count == 2);

        C c = C(b);
        writeln(a.count == 3);
}

Reply via email to