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?

Reply via email to