`static` symbol needs to be `immutable` for compile-time access?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 22 02:15:19 PST 2016


On Friday, 22 January 2016 at 09:56:27 UTC, Shriramana Sharma 
wrote:

> In C/C++ the `static` here is used to avoid the array being 
> created every time the function is entered; in D too it does 
> the same thing, no? So if I have an array of constants in a 
> function that I need to be accessible to a template at compile 
> time, and I (for obvious reasons) don't want to be initialized 
> at every function call, do I have to declare it `static 
> immutable`?

A static variable is still a runtime variable. It's effectively 
the same as declaring a variable outside of the function scope at 
module scope, except that it's visible only in the current scope 
and the function name gets mangled into the symbol

int i;

void foo() {
    static int j;
}

j is no more a compile-time value than i is. If you want an array 
of constant values available at compile-time, then you need to 
declare the array as immutable.


More information about the Digitalmars-d-learn mailing list