Compile-time array initialization
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).
I'm not sure how to do it. Can you give an example?
Remember I want the initialized array available at compile-time.
Remember I want the initialized array available at compile-time.
edit: missread the question.
I belive the book accompanying loki explains how to do stuff like this.
Check his Andrei Alexandrescu website
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�
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.
Quote:So you want the 0th element to hold infinity? ;)
Ok, that was not a very good example :)
This is what I've managed to do:
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?
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?
Slightly better:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement