C++ guys hate static_if?

Timon Gehr timon.gehr at gmx.ch
Mon Mar 11 17:10:06 PDT 2013


On 03/11/2013 11:06 PM, H. S. Teoh wrote:
> On Mon, Mar 11, 2013 at 10:31:53PM +0100, 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.
>
> Actually, it doesn't, scarily enough:
>
> 	import std.stdio;
> 	void main() {
> 		int[string] x;
> 		//float x;
> 		static if (is(typeof(x) S : T[U], T, U)) {
> 			writeln(S.stringof);
> 			writeln(T.stringof);
> 			writeln(U.stringof);
> 		}
> 		writeln(S.stringof); // <-- this compiles, and works!!
> 	}
>
> The last writeln will fail to compile if x's type is changed to float
> (as in the commented out line).
>
> Meaning that the definitions of S, T, U "leak" past the scope of the
> static if. Which makes the semantics of the code very unclear, because
> whether or not it even compiles depends on how the static if condition
> turns out. :-/
>
>
> T
>

Wtf. :)

void main() {
     int[string] x;
     static if (is(typeof(x) S : T[U], T, U)) { }
     pragma(msg, S, " ", T, " ", U);
}

I have checked again and the documentation actually does not specify 
this. It just contains suggestive examples.


More information about the Digitalmars-d mailing list