template specialization for arrays

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Oct 30 07:36:26 PDT 2011


On 10/30/11 5:16 AM, J Arrizza wrote:
>         You should use either std.traits.isXxx systematically, or
>         patterns systematically, but not both at the same time.
>         Personally I prefer isXxx because they foster simple logic to
>         decide what overloads should apply.
>
>
>     Also, when posting, you may want to include complete short programs
>     so others can try them quickly.
>
>
> Andrei, I thought I had posted the entire program. Here it is again
> using only traits as you recommend:
>
>     import std.stdio;
>     import std.traits;
>     void abc(T) (T parm1)
>        if (isNarrowString!T || (!isStaticArray!T && !isDynamicArray!T))
>        {
>        writeln("simpleparm: ", parm1);
>        }
>     void abc(T) (T parm1)
>        if (!isNarrowString!T && (isDynamicArray!T || isStaticArray!T) )
>        {
>        writeln("array : ", parm1);
>        }
>     void main(string[] args)
>        {
>          writeln("v4");
>          abc(1);
>          abc("str");
>          int[] arr = [1, 2];
>          abc(arr);
>          int[2] arr2 = [3, 4];
>          abc(arr2);
>        }

Thanks, sorry for having missed that.

The code as above is canonical. I think restricted templates are the way 
to go for most code. Pattern matching on types is rather arcane and 
should be let to a few advanced uses (such as implementing traits 
themselves).


Andrei


More information about the Digitalmars-d mailing list