C++ guys hate static_if?

Jonathan M Davis jmdavisProg at gmx.com
Mon Mar 11 13:19:15 PDT 2013


On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
> Actually, in D, static if creates its own scopes for declarations made
> inside the static if condition.

No, it doesn't, and it would be _way_ less useful if it did, particularly with 
regards to struct and class definitions. Take this code, for instance,

import std.stdio;

static if(true)
    int var = 7;
else
    string var = 12;

void main()
{
    int i = var;

    static if(is(typeof(var) == int))
        int j = 22;
    else
        float j;

    writeln(j);
}

It compiles just fine and prints 22. Both the static if at module-level and the 
one in the function declare variables which are used outside of the static if 
blocks. static if does _not_ create a new scope. And putting braces around the
static if bodies has no effect on the scoping either.

- Jonathan M Davis


More information about the Digitalmars-d mailing list