cast(immutable) vs extra variable
Namespace
rswhite4 at googlemail.com
Thu Sep 19 09:50:31 PDT 2013
On Thursday, 19 September 2013 at 16:47:13 UTC, Daniel Davidson
wrote:
> 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));
> }
cast(immutable)data) is not an lvalue, it's a rvalue. ref accepts
only lvalues.
More information about the Digitalmars-d-learn
mailing list