[phobos] swap()

Martin Nowak dawg at dawgfoto.de
Sun Jan 2 09:19:28 PST 2011


Well, bug 4789 is fixed for static arrays but reintroduced for structs  
with elaborate assign.
Array copy is not allowed for overlapping memory.

           ubyte[T.sizeof] t = void;
           auto a = (cast(ubyte*) &lhs)[0 .. T.sizeof];
           auto b = (cast(ubyte*) &rhs)[0 .. T.sizeof];
           t[] = a[];
           a[] = b[]; <-- will raise exception
           b[] = t[];

Martin

On Sun, 02 Jan 2011 00:42:44 +0100, Andrei Alexandrescu
<andrei at erdani.com> wrote:

> This has been lying in my inbox for a good while. I see now the bug is  
> fixed. Any trailing questions regarding it?
>
> Andrei
>
> On 9/2/10 10:58 PM, David Simcha wrote:
>> A few questions about std.algorithm.swap() that came up when I was
>> reading the code to fix Bug 4789:
>>
>> 1. Do we really need to use memcpy() for structs w/o a postblit? I can't
>> see any advantages and it seems like it might be less efficient, since
>> for a struct without a postblit just using plain old assignment should
>> be about as efficient as it gets.
>>
>> 2. memcpy() isn't guaranteed to work for overlapping memory blocks.
>> Shouldn't there be a check in to make sure a and b don't have the same
>> address?
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list