Jump to page: 1 2
Thread overview
variadic args
Dec 07, 2006
novice2
Dec 07, 2006
JohnC
Dec 08, 2006
JohnC
Dec 07, 2006
Kirk McDonald
Dec 08, 2006
Bill Baxter
Dec 08, 2006
novice2
Dec 11, 2006
novice2
Dec 11, 2006
Aarti_pl
Dec 11, 2006
Endea
Dec 11, 2006
Aarti_pl
Dec 11, 2006
Bill Baxter
Dec 11, 2006
Bill Baxter
Dec 12, 2006
novice2
Dec 12, 2006
Bill Baxter
Dec 12, 2006
novice2
December 07, 2006
can i pass variadig arguments from one function to other? siomething like:

void func1(...)
{
  func2(...);
}

is it still not possible?

(sorry, i was asked this some time ago. but now i see array literals are mnaked and other good changes - may be this question changed too.)
December 07, 2006
"novice2" <sorry@nom.ail> wrote in message news:el9brb$lug$1@digitaldaemon.com...
> can i pass variadig arguments from one function to other? siomething like:
>
> void func1(...)
> {
>  func2(...);
> }
>
> is it still not possible?
>
> (sorry, i was asked this some time ago. but now i see array literals are mnaked and other good changes - may be this question changed too.)

This is what I do:

import std.stdarg;

void resolveArgs(inout TypeInfo[] arguments, inout void* argptr) {
  if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) &&
arguments[1] is typeid(void*)) {
    arguments = va_arg!(TypeInfo[])(argptr);
    argptr = *cast(void**)argptr;

    if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) &&
arguments[1] is typeid(void*))
        resolveArgs(arguments, argptr);
  }
}

void func1(...) {
  resolveArgs(_arguments, _argptr);
  func2(_arguments, _argptr);
}

void func2(...) {
  resolveArgs(_arguments, _argptr);
  // Do something with the args
}


December 07, 2006
novice2 wrote:
> can i pass variadig arguments from one function to other?
> siomething like:
> 
> void func1(...)
> {
>   func2(...);
> }
> 
> is it still not possible?
> 
> (sorry, i was asked this some time ago. but now i see array
> literals are mnaked and other good changes - may be this question
> changed too.)

The new variadic templates make this easy:

void func1(T ...)(T t) {
    func2(t);
}

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
December 08, 2006
Kirk McDonald wrote:
> novice2 wrote:
>> can i pass variadig arguments from one function to other?
>> siomething like:
>>
>> void func1(...)
>> {
>>   func2(...);
>> }
>>
>> is it still not possible?
>>
>> (sorry, i was asked this some time ago. but now i see array
>> literals are mnaked and other good changes - may be this question
>> changed too.)
> 
> The new variadic templates make this easy:
> 
> void func1(T ...)(T t) {
>     func2(t);
> }
> 

... if you don't mind compile-time variadic behavior.  I.e. can't pass func1 around as a function pointer, can't make it a polymorphic member in a class, you don't mind different code being generated for every place you call it, don't mind making the function's definition available as source code ... etc.

But if you are ok with that then, yeh, variadic templates work quite nicely.

--bb
December 08, 2006
thanks for all replies!
resume: basically nothing changed yet,
no language support for this (yet?).
December 08, 2006
JohnC wrote:

> This is what I do:
> 
> import std.stdarg;
> 
> void resolveArgs(inout TypeInfo[] arguments, inout void* argptr) {
>   if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) && arguments[1] is typeid(void*)) {
>     arguments = va_arg!(TypeInfo[])(argptr);
>     argptr = *cast(void**)argptr;
> 
>     if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) && arguments[1] is typeid(void*))
>         resolveArgs(arguments, argptr);
>   }
> }

This is not portable to GDC, however. (type of argptr should be va_list)

Comparing typeids with 'is' doesn't always work but using '==' should...

--anders
December 08, 2006
"Anders F Björklund" <afb@algonet.se> wrote in message news:elbfah$2u9i$1@digitaldaemon.com...
> JohnC wrote:
>
>> This is what I do:
>>
>> import std.stdarg;
>>
>> void resolveArgs(inout TypeInfo[] arguments, inout void* argptr) {
>>   if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) &&
>> arguments[1] is typeid(void*)) {
>>     arguments = va_arg!(TypeInfo[])(argptr);
>>     argptr = *cast(void**)argptr;
>>
>>     if (arguments.length == 2 && arguments[0] is typeid(TypeInfo[]) &&
>> arguments[1] is typeid(void*))
>>         resolveArgs(arguments, argptr);
>>   }
>> }
>
> This is not portable to GDC, however. (type of argptr should be va_list)

Oh right, I didn't know that.

>
> Comparing typeids with 'is' doesn't always work but using '==' should...

It should, but doesn't in my experience.

>
> --anders


December 08, 2006
"novice2" <sorry@noem.ail> wrote in message news:elbedj$2t21$1@digitaldaemon.com...
> thanks for all replies!
> resume: basically nothing changed yet,
> no language support for this (yet?).

Nope.  Though the "standard" method to do this, kind of carried over from C, is to have a 'v' version of each function, and then a truly variadic version which just calls the 'v' version.  In this way you can pass the variadic args to the other 'v' functions.

void vFoo(TypeInfo[] arguments, va_list argptr)
{
    // do the actual body of the function here
    vBar(arguments, argptr);
}

void foo(...)
{
    vFoo(_arguments, _argptr);
}

void vBar(TypeInfo[] arguments, va_list argptr)
{

}

void bar(...)
{
    vBar(_arguments, _argptr);
}


December 11, 2006
many thanks, Jarrett Billingsley.
i don't like templates, so you solution good.
i hope, Walter will add support for this in language,
and such ugly construction will gone from sources in future.
December 11, 2006
novice2 napisał(a):
> many thanks, Jarrett Billingsley.
> i don't like templates, so you solution good.
> i hope, Walter will add support for this in language,
> and such ugly construction will gone from sources in future.

You can try with Any ported to D. It allows quite nice solution for variadic arguments:

void printVector(Any[] vec) {
	// printing vector of different type arguments
	// see Any description and code; eventually boost any 	
	// documentation
}

void printVariadic(Any[] var...) {
	printVector(var);
}

void main() {

	Any[] vec;

	vec~=(new Any).assign(6);
	vec~=(new Any).assign("Text"[]);
	vec~=(new Any).assign(5.5);

	printVector(vec);

	auto v = new Any;
	auto z = new Any;
	v.assign(2.5);
	z.assign("Another text"[]);

	printVariadic(v, z);

}


I will send at the end of week updated version of Any, which supports new opAssign() overloaded operator, so it will be possible:

	v=2.5;
instead of
	v.assign(2.5);

Using Any you do not have to use va_list, TypeInfo, pointers etc. It's just redundant then...

I just would wish myself that Walter will add support for templatized constructors to achieve following:
	# printVariadic(new Any(4), new Any("Yet another text"[]), new Any(3.14)); // it doesn't work now...

Best Regards
Marcin Kuszczak
« First   ‹ Prev
1 2