Re: how to instantiate array of objects knowing its indices at compile
time?
Victor Bazarov wrote:
Christof Warlich wrote:
Victor Bazarov schrieb:
But that would make every Element a unique type (at the first glance,
anyway)! And that would mean you can't convert between them (unless
you make them all descendants of each other, which is ugly) or put
them all in the same collection/container...
You are right, but I could live with that, i.e. I do not need to convert
between them.
But what I need is some usable indexing:
Array<4>::Array<3>::Array<2>::Element::index is not a working solution
to access the value of index 2.
Can you show how you'd like to use the indexing? I know it's not
working, but I would need to see what interface you're looking for.
OK, I thought of something, but this might be more complicated than you
need:
#include <iostream>
#include <ostream>
template<unsigned s, class T> struct ArrayOf;
template<class T> struct ArrayOf<0,T> {}; // empty
template<class T, unsigned ind> struct ArrayElement
{
enum { index = ind };
T data;
ArrayElement() : data() {} // requires T to be default-consructible
ArrayElement(T d) : data(d) {} // requires T to be copy-constructible
};
template<class T> struct ArrayOf<1,T>
{
ArrayElement<T,0> element; // will be default-constructible
T operator[](unsigned) const { return element.data; }
T& operator[](unsigned) { return element.data; }
};
template<unsigned size, class T> struct ArrayOf
{
enum { lastindex = size-1 };
ArrayElement<T,lastindex> tail;
ArrayOf<size-1,T> head;
T operator[](unsigned i) const {
if (i < lastindex)
return head.operator[](i);
else
return tail.data;
}
T& operator[](unsigned i) {
if (i < lastindex)
return head.operator[](i);
else
return tail.data;
}
};
int main() {
ArrayOf<10,int> myArrayOf10ints;
for (int i = 0; i < 10; ++i)
myArrayOf10ints[i] = i+42;
for (int j = 3; j < 8; ++j)
std::cout << "# " << j;
std::cout << " is " << myArrayOf10ints[j] << std::endl;
}
This, of course, creates 20 different types (arrays of all sizes from 1
to 10, and elements with all different indices from 0 to 9). I imagine
that if you need an array of 1000 elements, your compiler might choke on
the compilation recursion.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask