Strange behavior when concatenating array

Jose Armando Garcia jsancio at gmail.com
Thu Jun 16 21:05:56 PDT 2011


It looks like the rt is not calling the postblit constructor when
concatenating arrays. For example, the following code:

import std.stdio;

struct Test
{
   this(this) { writeln("copy done"); }
   void opAssign(Test rhs) { writeln("assignment done"); }
   ~this() { writeln("destructor called"); }
}

void main()
{
   Test[] tests = new Test[1];
   {
      Test test;
      tests ~= test;
   }
   writeln("done");
}

Gives the following output:

destructor called
done

The dtr for 'Test test;' is getting call after the scope exits but the
postblit ctr for 'tests[0]' is never called. I believe the output of
this code should either be:

done

or:

copy done
destructor called
done

Thanks!
-Jose


More information about the Digitalmars-d-learn mailing list