Access template parameters at runtime

jerro a at a.com
Fri Aug 10 08:35:42 PDT 2012


>> Note that if your design makes that you must have a tuple, you 
>> may build
>> the array at compile time, so that you can index it at run 
>> time.
>
> That is what I was trying first, but I could not make it work. 
> Maybe you can show me how it's done?

This would be one way to do it:


auto staticArray(Elements...)(Elements elements)
{
     alias Elements[0] E;
     E[Elements.length] r;

     foreach(i, _; elements)
         r[i] = elements[i];

     return r;
}

struct SomeStruct(integers...) {
     enum arr = staticArray(integers);
     int opIndex(size_t idx){
         return arr[idx];
     }
}



More information about the Digitalmars-d mailing list