the struct when it is “@disable this(this)”, init the struct array use “T[] = T.init” it not work.
The S is @disable this(this); t[] = S.init. the t[0].i should be 42, but it was not.
import std.stdio;
import core.memory;
struct S { int i = 42; @disable this(this); }
struct D { int i = 24;}
void main()
{
writeln("@disable this(this);");
auto ptr = GC.malloc(S.sizeof * 5);
S[] t = cast(S[])(ptr[0..S.sizeof * 5]);
writeln(t[0].i);
t[] = S.init;
// foreach(ref ts ; t){ // this is work to 42
// ts = S.init;
// }
writeln(t[0].i); // it should be 42, but it not!
// assert(t[0].i == 42);
writeln("this(this);");
ptr = GC.malloc(D.sizeof * 5);
D[] d = cast(D[])(ptr[0..D.sizeof * 5]);
writeln(d[0].i);
d[] = D.init;
writeln(d[0].i);
writeln("this(this); D2");
ptr = GC.malloc(D.sizeof * 5);
D[] d2 = cast(D[])(ptr[0..D.sizeof * 5]);
writeln(d2[0].i);
D td;
td.i = 78;
d2[] = td;
writeln(d2[0].i);
assert(d[0].i == 24);
assert(d2[0].i == 78);
assert(t[0].i == 42);
}
当禁用复制构造的结构体时。对其数组进行初始化(T[] = T.init),会失败。
Comment #1 by robert.schadek — 2024-12-07T13:42:56Z