www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE - filling an array

reply The Anh Tran <trtheanh gmail.com> writes:
Hi,
I would like to pre-create a double array, each element is calculated by 
a function.

//-----------------------------------------------------
import std.stdio;
import std.conv;
import std.string;

template f(int N)
{
	bool fn(double[] tb)
	{
		for (int i = 1; i < N; i++)
			tb[i] = 1.0/i;
		return true;
	}
}

void main(string[] args)
{
	const int N = 200;
	static double[N] dd = void;
	static auto tmp = f!(N).fn(dd);
	
	int n = toInt(args[1]);
	
	writefln("test array[%d] = %f", n, dd[n]);
}
//------------------------------------------------------

dmd.2021 says: cannot evaluate "static auto tmp = f!(N).fn(dd);" at 
compile-time.

How can i fix it?
Thanks.
Dec 09 2008
parent Christopher Wright <dhasenan gmail.com> writes:
The Anh Tran wrote:
     static double[N] dd = void;

dd is not a compile-time constant.
     static auto tmp = f!(N).fn(dd);

The initializer of tmp must be a compile-time constant, but since dd is not a compile-time constant, you can't use CTFE on fn.
Dec 09 2008