[Issue 15662] Cannot move struct with defined opAssign due to @disabled post-blit

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Feb 18 04:33:55 PST 2016


https://issues.dlang.org/show_bug.cgi?id=15662

--- Comment #8 from Kenji Hara <k.hara.pg at gmail.com> ---
(In reply to Dicebot from comment #6)
> No objections here. Question is - how to implement it inside a library
> (without modifying compiler) if this array.length loophole is closed? Only
> thing that comes to my mind is to reinterpret cast it to array of
> `ubyte[T.sizeof]` and do manual blit copy but that feels very error-prone.

It would need some @trusted code and runtime check, because currently there's
not enough compile-time information to guarantee its safety.

My quick implementation:

void moveAppend(T)(ref T[] arr, T elem)
{
    if (!arr.capacity) throw new Error("cannot append");

    swap(*(arr.ptr + arr.length), elem);
    arr = arr.ptr[0 .. arr.length + 1];

    // *uninitialize* elem, as same as std.algorithm.move
    memcpy(&elem, typeid(T).initializer().ptr, sz);
}

--


More information about the Digitalmars-d-bugs mailing list