Function that calculates in compile time when it can

Minas Mina minas_mina1990 at hotmail.co.uk
Mon Aug 6 06:54:05 PDT 2012


Something like this:

template fib(ulong n)
{
	static if( n < 2 )
		const fib = n;
	else
		const fib = fib!(n-1) + fib!(n-2);
	
	if( n < 2)
		return n;
	return fib(n-1) + fib(n-2);
}

It doesn't work of course, as I am in a template and trying to 
"return" something.

CTFE? Is that "compile time function evaluation"? If yes, it's 
really slow...
If I try:
static x = fib(40); // fib is a normal function

it takes forever and makes my pc run really slowly.


More information about the Digitalmars-d-learn mailing list