Compile-time array initialization

Started by
20 comments, last by DigitalDelusion 19 years, 9 months ago
Is there a way to initialize an array of size N at compile time (where N is a constant integer specified as a template parameter)? E.g. if I wish the elements to be initialized to N^2/i (where i is the array index).

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

Advertisement
Wrap it in a templated class?
Create a templated function that returns such an array?
I'm not sure how to do it. Can you give an example?

Remember I want the initialized array available at compile-time.

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

edit: missread the question.
I belive the book accompanying loki explains how to do stuff like this.
Check his Andrei Alexandrescu website
have a look at this: http://www.gamedev.net/community/forums/topic.asp?topic_id=252723&whichpage=1&#1523241
Quote:Original post by Opwiz
Is there a way to initialize an array of size N at compile time (where N is a constant integer specified as a template parameter)? E.g. if I wish the elements to be initialized to N^2/i (where i is the array index).


So you want the 0th element to hold infinity? ;)
Quote:have a look at this: http://www.gamedev.net/community/forums/topic.asp?topic_id=252723&whichpage=1

Nice :). Thank you.

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

Quote:So you want the 0th element to hold infinity? ;)

Ok, that was not a very good example :)

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

This is what I've managed to do:

template<int N>class ClassWithStaticArray{public:	void PrintElems()	{		std::cout << dummy << std::endl;			for(int i = 0; i < N; ++i)		{			std::cout << i << ": " << elem << std::endl;		}	}private:	template<int D>	static int Init()	{		return elem[D] = Init<D - 1>() - 1;	}	template<>	static int Init<0>()	{		return elem[0] = N;	}	static int elem[N];	static const int dummy;};template<int N>int ClassWithStaticArray<N>::elem[N];template<int N>const int ClassWithStaticArray<N>::dummy = Init<N>();


I have to use a static array but that happens to be what I need so that is not really a problem. The static array is initialized at run-time but only if the "dummy" variable is referenced. I want it to be generated at compile-time, or atleast without having to reference a dummy variable. Any ideas?

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

Slightly better:

template<int N>class ClassWithStaticArray{public:	void PrintElems()	{		for(int i = 0; i < N; ++i)		{			std::cout << i << ": " << ar << std::endl;		}	}private:	template<int D>	static int& Init()	{		return elem[D] = Init<D - 1>() - 1;	}	template<>	static int& Init<0>()	{		return elem[0] = N;	}	static int elem[N];	static int* ar;};template<int N>int ClassWithStaticArray<N>::elem[N];template<int N>int* ClassWithStaticArray<N>::ar = &Init<N>() - N;


Whenever I need to reference the array I use the pointer "ar" that gets initialized when first used. Still generated at run-time though. A cleaner run-time solution would be to initialize the array at the constructor and use a flag to ensure that it only gets initialized once.

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

This topic is closed to new replies.

Advertisement