Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding variable arguments in functions in D

Tags:

d

I have a function void foo(...) and a function void bar(...) and I want to call bar from foo, in a way that bar receives the same variable argument list than foo.

Is that possible in D?

Thanks!

like image 777
Santiago V. Avatar asked Feb 29 '12 12:02

Santiago V.


People also ask

Can we pass a variable argument list to a function at runtime?

Explanation: Every actual argument list must be known at compile time.

Can you pass va_list to another function?

The va_list may be passed as an argument to another function, but calling va_arg() within that function causes the va_list to have an indeterminate value in the calling function. As a result, attempting to read variable arguments without reinitializing the va_list can have unexpected behavior.

Where is __ Va_args __ defined?

The C standard mandates that the only place the identifier __VA_ARGS__ can appear is in the replacement list of a variadic macro. It may not be used as a macro name, macro argument name, or within a different type of macro. It may also be forbidden in open text; the standard is ambiguous.


2 Answers

if you use templates yes

void foo(A...)(A a){
    bar(a);
}

void bar(B...)(B b){
//...
}

the a gets expanded that compile time to what arguments it was called with

you can also slice[] off some arguments, or you add an argument to the list

like image 125
ratchet freak Avatar answered Oct 22 '22 09:10

ratchet freak


I think core.vararg might be of use.

like image 21
BCS Avatar answered Oct 22 '22 08:10

BCS