So my understanding is that structs have an implicit opAssign
which takes its argument by value, and that it only calls the destructor on its own copy, but not that of the argument, which may be a temporary.
For example:
struct S
{
this(int x) { writeln("ctor ", &this); }
~this() { writeln("dtor ", &this); }
}
void main()
{
S s;
writeln("main ", &s);
s = S(42);
}
the result, when compiled with DMD, is
main 7FFF28396290
ctor 7FFF28396291
dtor 7FFF28396268
dtor 7FFF28396290
There are three different addresses and the ctors and dtors involved in the copy and opAssign
disagree with each other.
Right now this prevents me from using &this
in ctors & dtors since, as far as temporaries are concerned, this is as if a dtor call is being elided by the compiler.
There is this bug report about it: https://issues.dlang.org/show_bug.cgi?id=9666
but I'm not sure of whether the differing addresses are part of what's intended.
Are they?
Also are there workarounds to get the ctors and dtors to see the same thing, other than compiling with LDC?