C++ guys hate static_if?

monarch_dodra monarchdodra at gmail.com
Mon Mar 11 15:12:45 PDT 2013


On Monday, 11 March 2013 at 21:31:53 UTC, Timon Gehr wrote:
> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>> 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,
>
> Yes it does.

No... It doesn't. He just showed you.

>> and it would be _way_ less useful if it did,
>
> I don't think so.

Half the code in phobos would be broken.

>> 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.
>>
>> ...
>
> What is the point? Your example code does not make any 
> declaration inside the static if condition.

What *are* you talking about??? I can count 4 declarations in 2 
static ifs? What is your definition of "declaration" and "scope".

There's a misunderstanding somewhere here. Can YOU show us an 
example where there is a declaration that is scoped?


More information about the Digitalmars-d mailing list