Recursive template problem

Lars Kyllingstad public at kyllingen.NOSPAMnet
Tue Jul 29 10:40:05 PDT 2008


Hello,

I want to make a template that evaluates to the summed absolute values 
of the elements of an array. In other words, I want something like this:

     real[3] foo;
     real sum = absSum!(foo);

to expand (at compile time) to

     real[3] foo;
     real sum = abs(foo[0]) + abs(foo[1]) + abs(foo[2]);

So, I wrote the following templates:

     private template absSum(real[N] V, ulong N) {
         real absSum = absSum!(V, N-1) + abs(V[N-1]);
     }
     private template absSum(real[N] V, ulong M, ulong N) {
         real absSum = absSum!(M-1, V) + abs(V[M-1]);
     }
     private template absSum(real[N] V, ulong M : 1, ulong N) {
         real absSum = abs(V[0]);
     }

but on compilation I get (for each template!) the error messages

   variable N forward referenced
   Error: Integer constant expression expected instead of N
   Error: Integer constant expression expected instead of N
   Error: Integer constant expression expected instead of N
   variable N forward referenced
   Error: Integer constant expression expected instead of N
   Error: Integer constant expression expected instead of cast(ulong)N
   Error: Integer constant expression expected instead of cast(ulong)N
   Error: arithmetic/string type expected for value-parameter, not real[N]

I haven't a clue why I am getting these messages. Any tips?

Thanks,

-Lars



More information about the Digitalmars-d mailing list