import std.stdio;
struct S {
this(int x) {
writeln("ctor");
}
ref S opAssign(ref const S s) {
writeln("assign");
return this;
}
~this() {
writeln("dtor");
}
}
S s;
static this() {
s = S(1); // (a)
s = S(1); // (b)
auto s2 = S(1);
s = s2; // (c)
}
void main()
{
}
(a) Constructor is correctly called on the "branded" struct.
(b) Bug: s is "branded" and constructed again. The original value doesn't get destructed. A correct behavior would be to try opAssign and fail because of the rvalue-to-ref mismatch.
(c) Bug: s2 is simply blitted to s. A correct behavior would be to call opAssign.
Comment #1 by nick — 2024-11-29T13:52:44Z
Yes, initializing a global from a static constructor should work like field initialization from a class constructor. That would also solve issue 24449.
Comment #2 by robert.schadek — 2024-12-13T19:07:58Z