Comment #0 by johannes.riecken — 2019-03-06T10:14:40Z
Code:
dmd -main -run -<<<'struct S {int i;}const S s;pragma(msg, s);'
Expected output:
S(0)
Actual output:
__stdin.d(1): Error: static variable s cannot be read at compile time
__stdin.d(1): while evaluating pragma(msg, s)
Note that initializing explicitly using a struct initializer literal gives the expected result:
dmd -main -run -<<<'struct S {int i;}const S s={};pragma(msg, s);'
S(0)
Comment #1 by simen.kjaras — 2019-03-06T11:09:49Z
This is intended behavior - a const variable with an initializer has a known value at compile time, while one without is expected to be initialized in a static constructor, and using its value before that point is undefined behavior.