Struct beeing moved around
Benjamin Thaut
code at benjamin-thaut.de
Fri May 20 10:36:58 PDT 2011
Am 20.05.2011 19:28, schrieb Michel Fortin:
> On 2011-05-20 10:30:33 -0400, Sean Kelly <sean at invisibleduck.org> said:
>
>> 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.
>
> Postblit is a post-copy constructor, not a post-move one. There is no
> such thing as a post-move constructor in D: structs are assumed to be
> movable.
>
> If the compiler was constructing the struct in place (instead of moving
> it) it'd be even better, but that's more a question of optimization than
> semantics.
>
What if I need a value type that may be copied, but may not be moved?
--
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d
mailing list