Arrays as template parameters

cal via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 4 16:25:11 PDT 2014


I have the following code (on dpaste, 
http://dpaste.dzfl.pl/636c04430a33):

enum : uint { a, b, c }
enum list = [a, b];

void foo(T...)()
{
   pragma(msg, T[0].length); // fine
   pragma(msg, T[0][0]);	    // fine
   pragma(msg, T[0][1]);	    // fine
	
   foreach(i; Iota!(0,T[0].length)) // fine
     pragma(msg, T[0][i]);

   //foreach(c; T[0]) // not fine
   //	pragma(msg, c);
}

template Iota(size_t i, size_t n)
{
   import std.typetuple : TypeTuple;
   static if (n == 0) alias TypeTuple!() Iota;
     else alias TypeTuple!(i, Iota!(i + 1, n - 1)) Iota;
}

void main()
{
   foo!list;
}

Just trying to pass a statically known array as a template 
parameter. The foreach marked 'not fine' doesn't work, saying c 
cannot be read at compile time, despite the length and all values 
being known at compile time. The foreach above it uses a template 
to generate a static tuple of indices, which are used to index 
into the array, which works but seems unnecessary.

Is there a better way to do this?


More information about the Digitalmars-d-learn mailing list