Function that calculates in compile time when it can
Marco Leise
Marco.Leise at gmx.de
Tue Aug 7 10:43:24 PDT 2012
Am Mon, 06 Aug 2012 14:21:37 +0200
schrieb "Minas Mina" <minas_mina1990 at hotmail.co.uk>:
> I want to write a fibonacci(n) function that calculates the
> result.
> a) if n is known at compile time, use a template
> b) if not, use a normal function
>
> I know how to write a template version:
> template fib(ulong n)
> {
> static if( n < 2 )
> const fib = n;
> else
> const fib = fib!(n-1) + fib!(n-2);
> }
>
> But how can I 'know' if n is known at compile time to make it use
> the other version? (which I won't post 'cause it is fairly easy).
That's easy: http://dpaste.dzfl.pl/521f47a0
--
Marco
More information about the Digitalmars-d-learn
mailing list