Safely moving structs in D

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 23 18:31:39 PST 2017


On Monday, 23 January 2017 at 23:04:45 UTC, Ali Çehreli wrote:
> On 01/23/2017 02:58 PM, bitwise wrote:
>> I'm confused about what the rules would be here.
>>
>> It would make sense to call the postblit if present, but 
>> std.Array
>> currently does not:
>> https://github.com/dlang/phobos/blob/04cca5c85ddf2be25381fc63c3e941498b17541b/std/container/array.d#L884
>>
>
> Post-blit is for copying though. Moving should not call 
> post-blit. You may want to look at the implementation of 
> std.algorithm.move to see how it plays with post-blit:
>
>   https://dlang.org/phobos/std_algorithm_mutation.html#.move
>
> Ali

That's a good point.

It didn't click at first, but checking for postblit is done with 
'hasElaborateCopyConstructor(T)'.

I had thought that what memmove was doing would be considered 
"blitting", and hence require a postblit afterwards.

I did look at std.move, but was mistaken about which code path 
was being taken.
It seemed like structs that defined only a postblit would have 
been moved by assignment:

https://github.com/dlang/phobos/blob/366f6e4e66abe96bca9fd69d03042e08f787d040/std/algorithm/mutation.d#L1310

But in actuality, the memcpy branch fires because 
hasElaborateAssign(T) returns true for structs with a postblit - 
which was unexpected. I don't really understand why, but this 
makes things clearer.

   Thanks



More information about the Digitalmars-d-learn mailing list