Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Template for Variable Arguments

Part of TemplatesCategory

Description

Template to facilitate using variable argument lists

Example

private import std.c.stdio;
template va_array(T)
{
   T[] va_array(uint* ptr)
   {
      T* args = cast(T*)(cast(va_list)ptr + uint.size);
      return args[0 .. *ptr];
   }
}

template new_array(T)
{
   T[] new_array(uint n,...)
   {
      return va_array!(T)(&n).dup;
   }
}

// user code
void test(int[] x)
{
   printf("last element: %d\n",x[x.length-1]);
}


int main()
{
   test(new_array!(int)(3, 1,2,3));
   return 0;
}

Source

From D:25034 by Ben Hinkle.