Jump to content
  • Advertisement
Sign in to follow this  
Opwiz

Compile-time array initialization

This topic is 5125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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).

Share this post


Link to post
Share on other sites
Advertisement
Wrap it in a templated class?
Create a templated function that returns such an array?

Share this post


Link to post
Share on other sites
I'm not sure how to do it. Can you give an example?

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
have a look at this: http://www.gamedev.net/community/forums/topic.asp?topic_id=252723&whichpage=1�

Share this post


Link to post
Share on other sites
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? ;)

Share this post


Link to post
Share on other sites
Quote:
have a look at this: http://www.gamedev.net/community/forums/topic.asp?topic_id=252723&whichpage=1

Nice :). Thank you.

Share this post


Link to post
Share on other sites
Quote:
So you want the 0th element to hold infinity? ;)

Ok, that was not a very good example :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!