Variadic functions: How to pass another variadic function the variadic args?
Gabi
galim120 at bezeqint.net
Sat Aug 3 12:00:02 PDT 2013
On Saturday, 3 August 2013 at 18:48:17 UTC, monarch_dodra wrote:
> On Saturday, 3 August 2013 at 16:57:41 UTC, Gabi wrote:
>> On Saturday, 3 August 2013 at 14:58:49 UTC, bearophile wrote:
>>> Gabi:
>>>
>>>> //HOW TO pass F1(..) the args we were called with ?
>>>
>>>
>>> import std.stdio;
>>>
>>> void f1(Args...)(Args args) {
>>> foreach (arg; args)
>>> arg.writeln;
>>> }
>>>
>>> void f2(Args...)(Args args) {
>>> f1(args);
>>> }
>>>
>>> void main() {
>>> f2(10, "hello", 1.5);
>>> }
>>>
>>>
>>> Bye,
>>> bearophile
>>
>> Thanks but how do I do this for variadic functions (using
>> _arguments and friends) ? Not variadic templates..
>> More specifically I want to know at runtime the type of each
>> parameter
>
> I don't know how to do runtime variadics in D, and quite
> frankly, I don't want to know: They are an abomination, and I
> don't want to be anywhere near these things.
>
> My guess though, is that it's the same syntax as in C? Use a
> straight up elispis:
>
> void foo(...).
>
> Note that you *can't* extract the types from the vararg unless
> you *guess* them from an alternative source (for example, "fmt"
> in the printf function)
>
> Also, importing "core.vararg" should get you whatever you'd get
> in 'vararg.h'/"stdarg.h". From there, I don't think D does
> anything specific that's not actually just C.
Ok, I got an alternative solution. What do you think about the
following ?
void f(T:long) (T arg)
{
...
}
void f(T:int) (T arg)
{
...
}
void f(T:int) (T arg)
{
...
}
void f(T:string) (T arg)
{
...
}
void f1(ARGS...)(ARGS args)
{
foreach(arg; args)
{
f(arg);
}
}
More information about the Digitalmars-d-learn
mailing list