23.08.2010 16:31, bearophile wrote:
Stanislav Blinov:

I was wondering if anyone has suggestions on performing arbitrary
initialization of static arrays, size of which is arbitrary at compile time.
Please explain your purposes a bit better.

I have a struct template (let's call it S) that wraps an array of N elements. N is set at compile time via template parameter. S uses static array as storage (T[N]). I need a set of constants of type S which I'd like to be evaluatable at compile time. I can create these constants with the following constructs:

static immutable S C1 = { /* initialization of struct fields here */ };
static immutable S C2 = { /* initialization of struct fields here */ };

Hence, I need some way to initialize a field which is T[N].

Later, I could use those constants like this:

class Foo
{
    S s1_ = S.C1;
    S s2_ = S.C2;
}

I can write a set of initializers for some values of N, but I'd like them to be generic so that I could apply them for arbitrary value of N. E.g. one can do things like T[3] a = [ 1, 2, 3 ], but I'd like to be able to do T[N] = SomeInitializerForArrayOfNElements;

What I'm trying to achieve is:

1. Initialize T[N] elements to a specific value.
2. Initialize every element of T[N] to some value deduced at compile time using it's index.

I came up with the templates in my initial post. They seem to work, but I doubt those are legal solutions.

Reply via email to