Postblit isn't called on rvalue return
Sebastian Graf
SebastianGraf at t-online.de
Wed Apr 24 04:41:59 PDT 2013
For this program:
import std.stdio;
struct S
{
ubyte* b;
ubyte buf[128];
this(this)
{
writeln("postblit");
}
}
auto ref makeS()
{
S s;
s.b = s.buf;
writeln("made S at ", cast(void*)&s, ", s.b == ", s.b);
return s;
}
void main()
{
S s = makeS();
writeln("got back S at ", cast(void*)&s, ", s.b == ",
s.b);
}
I get
made S at 18FC64, s.b == 18FC68
got back S at 18FCF4, s.b == 18FC68
as output for dmd 2.062. Patching s.b to point into the newly
allocated struct in postblit is crucial here, but it seems like
the postblit constructor isn't called, nor is there any attempt
to optimize away the temporary in `makeS()` even with -O.
Is this is a bug or am I doing something wrong?
More information about the Digitalmars-d-learn
mailing list