Detecting array type without template specialization?

Sean Kelly sean at f4.ca
Tue May 9 13:33:45 PDT 2006


Jarrett Billingsley wrote:
> Say I want a template to do different things if it's passed an array versus 
> a non-array type.  I can do this easily with template specialization:
> 
> template Spoon(T)
> {
>     void knife()
>     {
>         writefln("single");
>     }
> }
> 
> template Spoon(T : T[])
> {
>     void knife()
>     {
>         writefln("array");
>     }
> }
> 
> ...
> 
> mixin Spoon!(int) i;
> mixin Spoon!(int[]) a;
> 
> i.knife();
> a.knife();
> 
> 
> That prints
> 
> single
> array
> 
> As I'd like it to.
> 
> That works great, but what if I'm writing a much larger class, where most of 
> the functionality would be the same between the array and non-array 
> versions, and I only want to change, say, one function to work differently 
> for arrays?  It would be redundant and cumbersome to have to copy most of 
> the contents of one template to the other.
> 
> Something like this can be done for classes versus non-classes:
> 
> template Spoon(T)
> {
>     void knife()
>     {
>         static if(is(T : Object))
>             writefln("class");
>         else
>             writefln("non-class");
>     }
> }
> 
> But is there any way to do this for array types? 

template isArrayType( T )       { const bool isArrayType = false; }
template isArrayType( T : T[] ) { const bool isArrayType = true;  }

template Spoon( T )
{
     void knife()
     {
         static if( isArrayType( T ) )
             writefln( "array" );
         else
             writefln( "not array" );
     }
}

This sort of thing is why I created std.traits in Ares.


Sean



More information about the Digitalmars-d-learn mailing list