Above me @GoldRanger has accurately quoted the standard thus explaining the issue you're having. Technically since you have a variable in the initialization of k
it's considered non-constant
.
To add some context I'll put this here:
When the program compiles it puts the global variables in their own section of the binary (I'm not too familiar with linux or windows executable formats but on mac they are put in the __DATA segment). So when you put that line float k = 2.0f * (float) M_PI / wl;
the compiler isn't smart* enough to recognize that wl
is actually a constant at compile time and that you want k
to be initialized with the initial value of wl
.
*smart isn't exactly the right description here. In general since wl
isn't declared as const the expression isn't exactly a constant expression in general so the compiler doesn't know anything about it. I suppose maybe this is a semantics issue or possibly just me arguing with myself over the wording I used.
In order to do what you're trying to do I usually use a #define
for my initial constant that will be used throughout the program:
#define kwlconst 2.0f
float wl = kwlconst;
float k = 2.0f * (float) M_PI / kwlconst;