Iterate over symbols in tupleof without instance

Benjamin Thaut code at benjamin-thaut.de
Tue Apr 15 01:18:31 PDT 2014


I would like to iterate over T.tupleof at compiletime without having a 
instance of the given type. So fare the only way I found that works is 
the following

void iterateImpl(T, size_t i)()
{
   static if(i < T.tupleof.length)
   {
      // do something with T.tupleof[i]
      iterateImpl!(T, i+1)();
   }
}

void iterate(T)()
{
   iterateImpl!(T, 0)();
}

But this is quite ugly, as there will tons of stack frames on the stack 
during runtime. I really wanted something like:

void iterate(T)()
{

   foreach(size_t i, m; T.tupleof)
   // error message: "Need this to access <member name here>"
   {
      // do something with m
   }
}

Kind Regards
Benjamin Thaut



More information about the Digitalmars-d-learn mailing list