Array to argument list
Jarrett Billingsley
jarrett.billingsley at gmail.com
Thu Sep 11 07:29:54 PDT 2008
On Thu, Sep 11, 2008 at 9:39 AM, Simen Haugen <simen at norstat.no> wrote:
> I have an array, but I want to send them one by one to a function that has
> variadic arguments. Is there a way to do this at runtime?
>
> void p(A ...)(A Args) {}
> p([1, 2, 3]); // I want this to become p(1, 2, 3) somehow...
>
Hm. My guess is that in general, "no", since you need to know how
many arguments to call p with at compile time, but you don't know how
many you'll need until runtime. You'd have to generate size_t.max
instantiations of p to cover all possible array lengths, as well as
size_t.max instantiations of a helper function to convert an array
into a tuple.
However for a restricted case -- fixed-size arrays -- it's possible.
But again, the number of instantiations is proportional to the length
of the array.
Here's an ugly, completely un-transparent solution.
import tango.core.Tuple;
import tango.io.Stdout;
struct Tup(T...)
{
T vals;
}
Tup!(T) makeTup(T...)(T args)
{
Tup!(T) ret;
ret.vals = args;
return ret;
}
template Rep(int num, V...)
{
static if(num == 0)
alias Tuple!() Rep;
else
alias Tuple!(V[0], Rep!(num - 1, V)) Rep;
}
Tup!(Rep!(N, U)) toTuple(T: U[N], U, int N)(T arr)
{
Tup!(Rep!(N, U)) ret;
foreach(i, v; ret.vals)
ret.vals[i] = arr[i];
return ret;
}
void p(A...)(A args)
{
foreach(i, arg; args)
Stdout.formatln("Arg {}: {}", i, arg);
}
void main()
{
int[3] arr = [1, 2, 3];
p(toTuple(arr).vals);
}
More information about the Digitalmars-d-learn
mailing list