Bug 1268 – Struct literals try to initialize static arrays of non-static structs incorrectly
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-06-15T11:33:00Z
Last change time
2014-02-16T15:23:52Z
Keywords
diagnostic, rejects-valid
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla
Comments
Comment #0 by matti.niemenmaa+dbugzilla — 2007-06-15T11:33:59Z
struct S {
int[5] x;
}
void main() {
S s = S();
}
The above code fails to compile with the following errors:
Error: cannot implicitly convert expression (0) of type int to int[5]
Error: cannot cast int to int[5]
(Notice the lack of line numbers.)
The "S s = S();" line works at global scope, or when preceded by const or static.
The only way to circumvent this is to add an initializer for each array element:
struct S {
//int[5] x = 0; // what the compiler tries: doesn't work
//int[5] x = []; // void[0], not int[5]
//int[5] x = cast(int[5])[]; // non-constant expression
//int[5] x = [0]; // int[1], not int[5] as above
// this works, but is tedious for long arrays
// (although it can be automated with templates)
int[5] x = [0,0,0,0,0];
}
Comment #1 by onlystupidspamhere — 2007-06-26T14:26:35Z