static if and templates

Don Clugston dac at nospam.com.au
Thu Sep 27 04:18:46 PDT 2007


Christian Kamm wrote:
>> what i do not understand is the following: if expr is a runtime construct,
>> how come  expr.mangleof works? for this to work (in my beginner thinking)
>> the compiler takes, say m, from main and inserts it into expr.mangleof.
>> Now, why is it not possible for the compile to insert the expr into
>> isArray? I think there is some fundamental thing i don't understand. Any
>> wisdom you can share?
> 
> Well, I'm pretty sure expr.mangleof becomes typeof(expr).mangleof, so
> 
> ---
> class A {}
> class B : A {}
> 
> A a = new B;
> writefln(a.mangleof);
> ---
> 
> Would give you the mangled name of A (compile time type) and not of B.
> 
> By the way, a more conventional way of writing your isArray and isMatrix
> functions would be
> 
> ---
> template isArray(T){
>   static if(is(T U : U[]))
>     const isArray = true;
>   else
>     const isArray = false;
> }

If you use:
    static if (is(typeof(T[0]))
it will work for user-defined types.


More information about the Digitalmars-d-learn mailing list