C++ guys hate static_if?
Era Scarecrow
rtcvb32 at yahoo.com
Mon Mar 11 15:42:25 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.
>> and it would be _way_ less useful if it did,
>
> I don't think so.
Oh? Examples inside the TDPL would break as the inner scopes
would be hidden and thereby inaccessible.
TDPL pg. 279
[quote]
[code]
template Select(bool cond, T1, T2) {
static if (cond) {
alias T1 Type;
} else {
alias T2 Type;
}
}
unittest {
alias Select!(false, int, string).Type MyType;
static assert(is(MyType == string));
}
[/code]
[/quote]
Plus it's part of the spec.
TDPL pg. 69
[quote]
Peeling Braces: There's a glaring oddity about the transmogrify
example. See, the numeric type is introduced inside a pair of {
and } braces. As such, it should be visible only locally inside
that scope (and consequently invisible to the enclosing
function), thus foiling our entire plan quite thoroughly. Such
behavior would also render the promised static if statement
practically useless. For that reason, the static if uses braces
for [i]grouping[/i] but not for [i]scoping[/i]. As far as scope
and visibility are concerned, static if peels the outermost
braces away, if any (they are not required when you have only one
controlled statement; out example above uses them only out of
stylistic obsession). If you do want braces, just add another
pair:
[code]
void main(){
static if (real.sizeof > double.sizeof) {{
auto maximorum = real.max;
writefln("Really big numbers - up to %s!", maximorum);
}}
... /* maximorum is invisible here */ ...
}
[/code]
[/quote]
More information about the Digitalmars-d
mailing list