N
npankey
I've started experimenting with template metaprogramming in a small
project of mine. What I'm trying to accomplish is to generate a static
array of templated objects that get specialized based on there
position in the array. This should be possible but I can't figure out
exactly how to accomplish this.
The below code should better illustrate what I'm trying to do:
template <int I>
class Item
{
int X() { /*do default */ }
}
template <>
class Item<0>
{
int X() { /* do something special */ }
};
template <int N>
class Array
{
// The following obviously won't work
// How do I push this type of initialization to compile time???
Array()
{
for(int i=0; i<N; i++)
{
data = Item<i>();
}
}
Item data[N];
}
int main()
{
Array<5> items;
/* do stuff with items */
return 0;
}
project of mine. What I'm trying to accomplish is to generate a static
array of templated objects that get specialized based on there
position in the array. This should be possible but I can't figure out
exactly how to accomplish this.
The below code should better illustrate what I'm trying to do:
template <int I>
class Item
{
int X() { /*do default */ }
}
template <>
class Item<0>
{
int X() { /* do something special */ }
};
template <int N>
class Array
{
// The following obviously won't work
// How do I push this type of initialization to compile time???
Array()
{
for(int i=0; i<N; i++)
{
data = Item<i>();
}
}
Item data[N];
}
int main()
{
Array<5> items;
/* do stuff with items */
return 0;
}