recursive function call at compile time
Oleg
code.viator at gmail.com
Sun Dec 16 09:33:09 PST 2012
Hello.
I want use recursion, but in my situation compiler doesn't
correct work.
import std.stdio;
struct MyStruct(uint K)
{
real[K] data;
auto getSmaller()
{
MyStruct!(K-1) ret;
foreach( no, ref d; ret.data )
d = data[no];
return ret;
}
real recursionAlgo()
{
static if( K == 1 ) return data[0];
else
{
real sum = 0;
foreach( i; 1 .. K )
sum += getSmaller().recursionAlgo();
return sum;
}
}
}
void main()
{
MyStruct!(5) a;
a.data = [ 1, 2, 3, 4, 5 ];
writeln( a.recursionAlgo() );
}
at compile time i have errors
./recursion.d(7): Error: index 4294967295 overflow for static
array
./recursion.d(7): Error: index 4294967294 overflow for static
array
.... etc
i think it's happens because compiler call 'recursionAlgo()'
where it should not be ( K == 1 )
how to turn off compile time calculation in this part of code?
More information about the Digitalmars-d-learn
mailing list