Template overloading

Regan Heath regan at netmail.co.nz
Mon Sep 17 03:33:19 PDT 2007


Oliver wrote:
> Hi everyone,
> 
> i would like to overload template functions. The following code shows what i'd like. 
> 
> import std.stdio;
> 
> void test(T1)(T1 n1) {
>     writefln("number: ", n1); }
> 
> void test(T2)(T2[] array ) {
>     writefln("array: ", array); }
> 
> 
> void main () {
>     double d1 = 2.1;
>     double d2 = 1.2;
>     double[] da1 = [d1, d2];
> 
>     test(d1);
>     test(da1);
> }
> 
> This fails (to my understanding) because the templates are not
> specialized enough. Is it possible to specialize the template to
> either accept an array of numbers (of any type) or a single number
> (of any type).
> 
> Any comments are appreciated. Thanks

My first instinct is to try this:

void test(T2 : T2[])(T2[] array ) {
     writefln("array: ", array); }

but, just like every other time I have tried to use template 
specialization I find that I cannot also use argument deduction:

template <file>.test(T : T[]) specialization not allowed for deduced 
parameter T

Granted the work around is simply to not use argument deduction but it's 
a real PITA.

Regan


More information about the Digitalmars-d-learn mailing list