static variables for non-constant expressions?

Andrej Mitrovic none at none.none
Sun Apr 10 18:24:42 PDT 2011


I was wondering why static variables can't have a run-time initializer.

For example this won't compile:
void foo(int input)
{
    static x = input;
}

But you can do it manually:
void bar(int input)
{
    static bool initted;
    static typeof(input) x;
    if (!initted) { x = input; initted = true; }
}

It's quite a bit more ugly. You also have to expand this for every new static variable that you write.

I don't know the background of how static variables really work, so is there a good reason why the first function can't work like the one below it?


More information about the Digitalmars-d-learn mailing list