Bug 2427 – Function call in struct initializer fails to compile
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-10-23T07:14:00Z
Last change time
2014-08-10T12:54:45Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
samukha
Comments
Comment #0 by samukha — 2008-10-23T07:14:15Z
Compiler tries to CTFE the function:
struct S
{
int x;
}
int foo(int i)
{
return i;
}
void main()
{
int i;
S s = { foo(i) };
return 0;
}
----
test.d(20): Error: variable i is used before initialization
Comment #1 by samukha — 2008-10-23T09:45:07Z
'return 0;' in the example is noise. please ignore it.
Comment #2 by bugzilla — 2008-10-26T04:41:36Z
The { } initializers for structs are for statically initialized data only. To use a dynamic initializer,
S s = S( foo(i) );
Comment #3 by samukha — 2008-10-26T05:14:48Z
From the spec:
"The static initializer syntax can also be used to initialize non-static variables, provided that the member names are not given. The initializer need not be evaluatable at compile time.
void test(int i)
{
S s = { 1, i }; // q.a = 1, q.b = i, q.c = 0, q.d = 7
}"
Comment #4 by github-bugzilla — 2014-08-10T12:54:34Z