When is array-to-array cast legal, and what does actually happen?

Ali Çehreli acehreli at yahoo.com
Mon Feb 22 14:51:04 PST 2010


Is the following cast legal with dmd 2.040?

struct MyChar
{
     dchar d;

     this(dchar param)
     in {
         assert(false);       // not called
     }
     body
     {
         d = param;
         assert(d != param);  // not called
     }

     this(this)
     in {
         assert(false);       // not called
     }
     body
     {
         assert(d != d);      // not called
     }
}

void main()
{
     MyChar[] mine = cast(MyChar[])("hello"d);   // legal?
     assert(mine[0].d == 'h');
}

I see that neither the constructor nor the postblit is called. 
Apparently the bit representation is used. This has the risk of 
violating struct invariants.

Is it legal?

Thank you,
Ali


More information about the Digitalmars-d-learn mailing list