Bug 11147 – Nested structs in a union are not correctly initialized
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-30T01:43:00Z
Last change time
2013-09-30T11:33:10Z
Keywords
industry, pull, wrong-code
Assigned to
nobody
Creator
andrea.9940
Comments
Comment #0 by andrea.9940 — 2013-09-30T01:43:43Z
DMD version: 2.063.2
The value of all variables in V should be zero, but the output shows they aren't.
---------------
struct V {
union {
struct {
float x = 1;
float y = 1;
float z = 1;
}
struct {
float r;
float g;
float b;
}
}
}
import std.stdio;
void main() {
writeln("V(", V.init.x, ", ", V.init.y, ", ", V.init.z, ", ", V.init.r, ", ", V.init.g, ", ", V.init.b, ")");
writeln(V.init);
writeln(V(V.init.x, V.init.y, V.init.z));
}
---------------
V(0, 0, 0, 0, 0, 0)
V(0, nan, nan, 0, nan, nan)
V(0, 0, 0, 0, 0, 0)
Comment #1 by andrea.9940 — 2013-09-30T01:47:05Z
> struct V {
> union {
> struct {
> float x = 1;
> float y = 1;
> float z = 1;
> }
> struct {
> float r;
> float g;
> float b;
> }
> }
> }
I posted the wrong code, the correct one is:
struct V {
union {
struct {
float x = 0;
float y = 0;
float z = 0;
}
struct {
float r;
float g;
float b;
}
}
}
Comment #2 by monarchdodra — 2013-09-30T03:36:34Z
I wanted to add that this:
void main() {
assert(V.init is V());
}
It would *appear* that the compiler is confused as to *what* the initial state of V is.