variadic function: passing args

Georg Wrede georg.wrede at nospam.org
Fri Jul 7 02:36:12 PDT 2006


David L. Davis wrote:
> In article <e8i4kn$133i$1 at digitaldaemon.com>, Chris Nicholson-Sauls
> says...
> 
>> icee wrote:
>> 
>>> In article <e8db27$1hu9$2 at digitaldaemon.com>, Walter Bright
>>> says...
>>> 
>>> 
>>>> icee wrote:
>>>> 
>>>> 
>>>>> is there a way to pass the ... args from one variadic
>>>>> function to another variadic function?
>>>>> 
>>>>> consider such case: void vf1(int a, ...) {
>>>>> 
>>>>> vf2(...); } void vf2(...) { }
>>>>> 
>>>>> can vf2 take _arguments and _argptr from vf1?
>>>> 
>>>> No, it works a lot like printf/vprintf. Check out the source to
>>>>  std.format for how to do it.
>>> 
>>> 
>>> 
>>> ---------------------- Yes, it works if we explicitly pass
>>> _arguments and _argptr to a overloaded ones, but it would be
>>> sweet if the compiler can do a magic for me, by some special 
>>> syntax. And even more sometime we have no control on the src
>>> which provides the vf2.
>>> 
>>> we can do this for a type safe variadic function in D(or C#?): 
>>> void vf1(int a, Object[] args...){ vf2(args); }
>>> 
>>> and in Python by pack/unpack: def vf1(a, *args, **kw): vf2(*arg,
>>> **kw) return
>>> 
>>> ----------------------
>>> 
>>> 
>>> In article <e8dc2l$1jta$1 at digitaldaemon.com>, Chris
>>> Nicholson-Sauls says...
>>> 
>>> 
>>>> Maybe if we /did/ have a way it would be better.  Maybe
>>>> something like: # vf2(_arguments ... _argptr);
>>>> 
>>>> Where the '...' in this case has become an operator meaning to
>>>> pass these varargs to the callee as such.  Not sure if it'd be
>>>> the best syntax, but its the simplest thing that comes to mind.
>>>> 
>>>> 
>>>> -- Chris Nicholson-Sauls
>>> 
>>> 
>>> ---------------------------- maybe just vf2(...); is OK??? if
>>> this tell the compiler wrap the _arguments _argptr pair of vf1
>>> into the vf2 and call it???
>>> 
>>> 
>>> and maybe i'm asking for too much:)
>>> 
>>> 
>> 
>> Well sure, but I thought it might be nice if one could, for
>> example, pop the first couple of values off before passing it.  Or
>> maybe be able to build one's varargs from scratch or other such odd
>> thing.  No I haven't done anything like that in the past... but it
>> could only be because I wasn't /able/ to do such a thing.  :)
>> 
>> -- Chris Nicholson-Sauls
> 
> 
> Chris,
> 
> You could check out my Diva Class project at:
> 
> http://spottedtiger.tripod.com/D_Language/D_Diva_Project_XP.html
> 
> Short description: -----------------
> 
> Diva (Dynamically Interconnected Variable Arguments) - Diva.d v0.38
> WIP Allows the ability to "get" and "set" data, plus alter D's
> variadic arguments function parameter layout with methods like
> "prepend, append, insert, splice, and remove.
> 
> Class implicitly converts stored values of non-exact types whenever
> possible, allowing for a polymorphic-like value passing to variables,
> otherwise it returns a default value, avoiding errors if all
> possible. The Diva Class is more focused toward flexablity (using
> automatic value conversion) than on opitmal (speed) peformance
> (operating on only exact/near-exact types i.e. std.boxer).

Damn, this is yet another thing that should be right in Phobos. (And 
Ares, etc. of course -- no offense!)

Maybe there should be a dsource project simply for gathering loose gems, 
polishing and uniforming them, and finally pushing them to the "default" 
libraries??

Such loose gems also exist in Python, CPAN, and actually pretty much 
everywhere. Some of them are quite easy to port to D.



More information about the Digitalmars-d mailing list