Access template parameters at runtime

Christophe Travert travert at phare.normalesup.org
Fri Aug 10 08:39:26 PDT 2012


"Henning Pohl" , dans le message (digitalmars.D:174572), a écrit :
> That is what I was trying first, but I could not make it work. 
> Maybe you can show me how it's done?

For example:

import std.stdio;

template TupleToArray(T...)
{
  static if (T.length == 1)
  {
    enum TupleToArray = [T[0]];
  }
  else
  {
    enum TupleToArray = TupleToArray!(T[0..$-1]) ~ T[$-1];
  }
}

void main()
{
  alias TupleToArray!(1, 2, 3) oneTwoThree;
  foreach (i; 0..3)
    writeln(oneTwoThree[i]);
}

output:
1
2
3

TupleToArray should have proper check to make clean code.
There must be something like that somewhere in phobos, or it should be 
added.

-- 
Christophe


More information about the Digitalmars-d mailing list