How to expand an expression along with a parameter tuple?

Artur Skawina art.08.09 at gmail.com
Mon Jun 17 04:54:49 PDT 2013


On 06/17/13 13:23, TommiT wrote:
> On Monday, 17 June 2013 at 11:15:24 UTC, Artur Skawina wrote:
>>    void bar(T...)(T values) {
>>       T tmp;
>>       foreach (i, ref v; values)
>>          tmp[i] = arr[v]*10;
>>       foo(tmp);
>>    }
> 
> Cool, I didn't know that you could create multiple variables like that (T tmp).

A more correct, but a bit less readable version (the types of 'values' and 'arr'
elements do not have to match) would be: 

   void bar(T...)(T values) {
      static if (T.length) {
         NTup!(T.length, typeof(arr[T[0].init])) tmp;
         foreach (i, ref v; values)
            tmp[i] = arr[v]*10;
         foo(tmp);
      }
      else
         foo();
   }
   
   template NTup(size_t N, T...) {
      static if (N>1)
         alias NTup = NTup!(N-1, T, T[$-1]);
      else
         alias NTup = T;
   }

artur


More information about the Digitalmars-d-learn mailing list