www.digitalmars.com         C & C++   DMDScript  

D - stdarg.h macro translation assistance

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
The following macros are defined in stdarg.h (DM):

[QUOTE]
typedef char __SS *va_list;
#define va_start(ap,parmn) ((void)((ap) =
(va_list)&(parmn)+__va_size(parmn)))
#define va_arg(ap,type)    (*(type __SS
*)(((ap)+=__va_size(type))-(__va_size(type))))
#define va_end(ap)    ((void)0)
[/QUOTE]

va_list is included in std.c.stdio but the rest are not.
I was wondering if someone would assist in the proper prototype of these
macros.
Here is what I came up with:


void va_start(va_list ap, void[] param)
{
  ap = (cast(va_list)&param+param.size;
}

va_arg // not sure what to make of this one

void va_end(va_list ap)
{
  ap = null;
}

I use them in a program that compiles and links correctly but does not works
as expected.
Not sure if it's because I'm missing va_arg or because I messed up defining
the other two.

Any help is appreciated,
Andrew
Apr 22 2004
parent reply "Walter" <walter digitalmars.com> writes:
Unfortuately, there's no way to do them in D. When I do it, I just code them
explicitly.
Apr 23 2004
parent reply Andrew Edwards <Andrew_member pathlink.com> writes:
In article <c6ajvr$2n6p$1 digitaldaemon.com>, Walter says...
Unfortuately, there's no way to do them in D. When I do it, I just code them
explicitly.

arguments in a variadic function? C used the format: type func(type arg1,...) the same mechanism D uses. As far as I understand it, the only reason for the first argument is to identify the starting address of the argument list. Once the starting point is identified, it can easily be ignored and, using the macro: va_args(va_list ap, type) it advances through the remaining arguments. Note that the type (type specifies in the case of printf()) is provided by the user. The only reason this method of access (ie, va_list, va_start, va_end, va_args) is used, is because no access method was built into the C language. I personally think D can do better than this. Why not provide an array mechanism [call it vargs instead of ...] that knows its starting address, and allows access to subsequent arguments through array indexing. I'm not sure what that entails in terms of designing a compiler but I'm sure will not be wasted time. Many on this forum have requested this feature, and I'm sure would be happily use it when it becomes available. If it cannot be done please explain why. Thanks, Andrew
Apr 23 2004
parent "Walter" <walter digitalmars.com> writes:
When I say it won't work in D, perhaps I was misleading. You certainly can
access variadic parameters in D exactly as you'd do in C, the problem is you
cannot create a *macro* to do it for you. If you write the macro expansions
in D, it will work fine.

I haven't expended much effort in this direction, because variadic argument
lists are rarely used (typically only for printf and scanf).
Apr 23 2004