Function that calculates in compile time when it can
Tobias Pankrath
tobias at pankrath.net
Mon Aug 6 06:46:13 PDT 2012
On Monday, 6 August 2012 at 12:21:38 UTC, Minas Mina wrote:
> 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).
What exactly do you try to accomplish? Why can't you use CTFE
instead of a template?
You can check for compile time with
static if(__ctfe)
More information about the Digitalmars-d-learn
mailing list