Struct beeing moved around

Sean Kelly sean at invisibleduck.org
Fri May 20 07:30:33 PDT 2011


On May 20, 2011, at 5:14 AM, Benjamin Thaut wrote:

> The following program:
> 
> import std.stdio;
> 
> struct test {
>  this(this){
>    writefln("postblit");
>  }
> 
>  int foo;
> 
>  this(int i){
>    foo = i;
>    writefln("%x",&foo);
>  }
> 
>  ~this(){
>    writefln("%x",&foo);
>  }
> }
> 
> void main(string[] args){
>     test t = test(5);
> }
> 
> 
> Gives me this output on dmd 2.052:
> 18fe58
> 18fe54
> 
> Is this a bug in 2.052? (Doesn't happen with 2.053)
> Why did the location of the struct change?
> Is there any way to get informed about a struct beeing moved?
> Is there a way to prevent it?

In main above you're declaring a new struct variable t which is default-constructed, then a temporary is created and initialized to 5, and then the temporary is copied onto t.  It does seem like a postblit should probably occur in this scenario though.



More information about the Digitalmars-d mailing list