static if and templates

Christian Kamm kamm.incasoftware at shift-at-left-and-remove-this.de
Thu Sep 27 11:55:21 PDT 2007


Don Clugston wrote:

>> 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.

Indeed that will accept anything that provides opIndex. You can check for
opSlice and the rest in a similar way, if your code requires them to be
available. With these checks, you can even skip the static if:

template hasIndexAndSlice(T) {
  const bool hasIndexAndSlice = is(typeof(T[0])) && is(typeof(T[0..0]));
}



More information about the Digitalmars-d-learn mailing list