D hackers requested. Dancing around postblit.
IgorStepanov via Digitalmars-d
digitalmars-d at puremagic.com
Wed Nov 5 12:19:02 PST 2014
On Wednesday, 5 November 2014 at 20:01:59 UTC, Adam D. Ruppe
wrote:
> If you're willing to make it @system or @trusted, you can just
> copy the data and call postblit manually:
>
> extern(C) @safe int printf(in char*);
>
> struct S {
> ~this() { printf("dtor\n"); }
> this(this) { printf("postblit\n"); }
>
> @disable S opAssign(S s);
> }
>
> S[] data;
> void emplace(Struct)(ref Struct val, size_t i) {
> // just blit the struct contents over
> // (same thing normal assign does anyway)
> (cast(ubyte*)&(data[i]))[0 .. Struct.sizeof] =
> (cast(ubyte*)&val)[0 .. Struct.sizeof];
> // call postblit
> data[i].__postblit();
> }
>
> void main() {
> data.length = 1;
> S s;
>
> emplace(s, 0);
> }
>
>
>
> Since it is a template, attribute inference should take care of
> nothrow, etc.
It was my first try :(
data[i].__postblit();
calls the user-defined postblit and only it.
struct Sx {
~this() { printf("dtor\n"); }
this(this) { printf("postblit\n"); }
@disable S opAssign(S s);
}
struct S
{
Sx s;
}
data[i].__postblit(); //error: S hasn't __postblit member, but it
need the postblit call for Sx
typeid(S).postblit(&data[i]) works as needed, but doesn't save
postblit attributes.
More information about the Digitalmars-d
mailing list