Comment #0 by simon.buerger — 2017-12-14T16:04:19Z
In the following example, CTFE does not correctly initialize the data field. Note that the bug disappears if either
- 'inout' is removed or
- 'Complex!double' is replaced by 'double' or
- 'data' is changed from 'Complex!double[1]' to 'Complex!double'
import std.complex;
import std.stdio;
struct Foo
{
Complex!double[1] data;
ref inout(Complex!double) opIndex(size_t i) inout
{
return data[i];
}
this(Complex!double a)
{
this[0] = a;
}
}
void main()
{
enum x = Foo(Complex!double(0));
const y = Foo(Complex!double(0));
writefln("%s", x.data[0]); // prints 'nan+nani' (incorrect)
writefln("%s", y.data[0]); // prints '0.0i' (correct)
}
Comment #1 by bitter.taste — 2018-03-18T20:47:55Z
Reduced to:
---
struct S
{
int x;
version (problem) void opAssign(S o) { x = o.x; }
}
struct Foo
{
S[1] data;
ref inout(S) g() inout { return data[0]; }
this(S a) { g() = a; }
}
void main()
{
enum x = Foo(S(42));
assert(x.data[0].x == 42);
}
---
The problem disappears if you remove `opAssign` (or the inout qualifiers).
Comment #2 by robert.schadek — 2024-12-13T18:55:32Z