Non-consistent implicit function template specializations

Ali Çehreli acehreli at yahoo.com
Tue Aug 17 18:46:05 UTC 2021


On 8/17/21 2:59 AM, Rekel wrote:

 > template TFoo(T)        { void foo(){writeln("1");} } // #1
 > template TFoo(T : T[])  { void foo(){writeln("2");} } // #2

I don't have such problems because I am not smart enough to understand 
that syntax so I don't use it. :) I use template constraints (which have 
other problems).

import std.traits;
import std.stdio;

template TFoo(T)
if (!isArray!T)
{
   void foo(){
     writeln("not array");
   }
}

template TFoo(T)
if (isArray!T)
{
   void foo(){
     writeln("array");
   }
}

void main() {
   TFoo!(int).foo();
   TFoo!(int[]).foo();
}

If you want 2 dimensional arrays, then you can use

import std.range;

   isArray!T && (isArray!(ElementType!T))

Ali



More information about the Digitalmars-d-learn mailing list