Compile-Time Functions - Allocating Memory?
Ary Borenszweig
ary at esperanto.org.ar
Tue Jul 22 19:46:15 PDT 2008
Era Scarecrow a écrit :
> I'm trying to compose as a test, where i fill a precompiled list of numbers in an array. What step am i missing to make this part work? (I i'm using gdc). it keeps complaining of a non-constant expression. Is there any way to make this work?
>
> bool []primes = tp(1000);
>
> bool [] tp( int max )
> {
> bool[] tmp = new bool[](max);
>
> for ( int cnt = 2; cnt < max; cnt++ )
> tmp[cnt] = isprime( cnt );
>
> return tmp;
> }
>
> bool isprime( int x )
> {
> for ( int cnt = 2; cnt < x; cnt++ )
> if ( x % cnt == 0 )
> return false;
>
> return true;
> }
>
> Era
lol,
Some days ago I was writing the same thing to make a video showing
Descent hovering over that constant and the primes would appear.
It seems this can't be interpreted:
new bool[](max);
You can't create anything on the heap when doing CTFE (afaik). I did it
by using a dynamic array:
bool[] tmp;
tmp ~= false;
tmp ~= false;
for ( int cnt = 2; cnt < max; cnt++ )
tmp ~= isprime( cnt );
And since CTFE seems to not have garbage collection, you might want to
do "cnt * cnt <= x" instead of "cnt < x" ;-)
More information about the Digitalmars-d-learn
mailing list