cast(immutable) vs extra variable

Daniel Davidson nospam at spam.com
Thu Sep 19 09:47:12 PDT 2013


Multi-part question:

1) Why does the last line fail? If cast to immutable how is it 
different than z? I know it is related to the ref. I'm using ref 
because I think it is likely more efficient - so assume the 
char[16] were really char[1024].

2) If I got rid of the ref, how many copies of the data would be 
made? And without looking at assembly what is a good way to 
answer this question? I've tried to add this(this) to other Stuff 
type structures to print when they are being called, but that 
does not work because no logic can be in the default constructor.

3) Also, is storing immutable(STUFF) in a struct in the general 
case (as opposed to just this one) useful or silly?

Thanks
Dan

     import std.stdio;

     alias char[16] Stuff;

     struct T
     {
       immutable(Stuff) myData;
       this(ref immutable(Stuff) data) {
         myData = data;
       }
     }

     void main() {
       immutable(Stuff) iData = "1234567890123456";
       Stuff data = "1234567890123456";
       writeln(T(iData));
       auto iData2 = cast(immutable)data;
       writeln(T(iData2));
       // WHY DOES THIS FAIL WHEN T(iData2) works?
       // writeln(T(cast(immutable)data));
     }



More information about the Digitalmars-d-learn mailing list