inner templates?

dennis luehring dl.soluz at gmx.net
Thu May 24 22:07:18 PDT 2007


Kirk McDonald schrieb:
> dennis luehring wrote:
>> hi
>>
>> i've try to write an template wich calculates e
>>
>> ok here it is - but wait i need a helper template to reduce the 
>> interface to my needs (just to adjust the precision) - is there any 
>> way getting rid of these helper template (maybe an inner-template or 
>> something)?
>>
>> -- 
>> import std.stdio;
>>
>> template e_help(double p, double s, double f, int n)
>> {
>>   static if( s > p )
>>     const e_help = e_help!(p, s/n,f+s/n,n+1);
>>   else
>>     const e_help = f;
>> }
>>
>> // this is the interface i want - but without the e_help template
>> template e(double p = 1e-14)
>> {
>>   const e = e_help!(p, 1, 1, 1);
>> }
>>
>> void main()
>> {
>>   writefln("e: ", e!());
>> }
>> -- 
>>
>> ciao dennis
> 
> Default arguments should do it in this case:
> 
> template e(double p=1e-14, double s=1, double f=1, int n=1)
> {
>   static if( s > p )
>     const e = e!(p, s/n, f+s/n, n+1);
>   else
>     const e = f;
> }
> 

i known default arguments - i use one in the e template... :-)
my problem is that i don't want the parameters s,f,n in the interface 
(because they are more internal stuff and someone can use them the wrong 
way)

function based with an recursive inner function to hide the internal 
stuff...

double fe(double p=1e-14)
{
   double fe_help(double p, double s=1, double f=1, int n=1)
   {
     if( s > p )
       return fe_help(p, s/n,f+s/n,n+1);
     else
       return f;
   }
   return fe_help(p);
}

it would be great to write the same code with templates...(normaly i 
port my code just over from functions to templates - except this code)

template fe(double p=1e-14)
{
   template fe_help(double p, double s=1, double f=1, int n=1)
   {
     if( s > p )
       const fe_help = fe_help!(p, s/n,f+s/n,n+1);
     else
       const fe_help = f;
   }
   return fe_help!(p);
}

proposal: we've got inner functions, inner classes - what speaks against 
inner templates? (except that the d compiler don't know the last one)


ciao dennis


btw: i know how to write an non recursive e calculator
without these dirty interface problems

double e(double p = 1e-14)
{
     int n=1;
     double s=1, f=1;
     while (s>p)
     {
         s=s/n;
         f=f+s;
         n=n+1;
     }
     return f;
}

but i want an recursive one



More information about the Digitalmars-d mailing list