copying a struct containing a struct

monarch_dodra monarchdodra at gmail.com
Fri Nov 16 14:24:08 PST 2012


On Friday, 16 November 2012 at 21:02:36 UTC, eskimo wrote:
>
> Btw. if you replace t1=t2; with t1.inner=t2.inner; everything 
> works as
> expected.

What you are seeing is move semantics in action:

When you call t1 = t2, it calls the compiler generated:
"opAssign(TTest other)"

As you can see, pass by value, so a new TTest is created by 
postblit, in the scope of main: "BFF5711C". This new copy is then 
*moved* into the scope of opAssign. At the end of opAssign, it is 
the moved object that is destroyed: "BFF570F4". As for the 
original object, since the compiler knows it was moved, it 
doesn't call the destroyer on it.

Then, at the end of the function, your t1 and t2 are destroyed.

--------
The fact that "opAssign called for: " is not printed is, AFAIK, a 
HUGE and old standing bug: The fields of the struct are bit 
copied (!)

Frankly, I have no idea why it isn't fixed yet...

To "bypass" this "bug", simply define an opAssign yourself.


More information about the Digitalmars-d mailing list