www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variable length arguement lists in D

reply "Ted Williams" <ted.wil.no.spam verizon.net> writes:
I have a stupid question: how does D handle variable length argument lists?
I've been over the D document and didn't see anything on it unless I missed
something.

Thanks,
Ted
May 21 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Ted Williams" <ted.wil.no.spam verizon.net> wrote in message
news:c8m1t8$1gan$1 digitaldaemon.com...
 I have a stupid question: how does D handle variable length argument
lists?
 I've been over the D document and didn't see anything on it unless I
missed
 something.
It handles them exactly as you'd handle them in C. You can also use std.c.stdarg.
May 21 2004
parent reply "Ted Williams" <ted.wil.no.spam verizon.net> writes:
I have tried to use the templates in the std.c.stdarg package.  I cant get
the code to compile.
The following snippet illustrates the problem:

import std.c.stdarg;

void TestArgs( char[] format, ... )
{
    va_list args;

    alias va_start!( char[] ) s;
    alias va_arg!(Object) a;

    s.va_start( args, format );
    Object o = a.va_arg( args );
}

When compiled I get the following errors:

args.d(11): function va_start (va_list ap,char[]parmn) does not match
argument types ()
args.d(11): Error: expected 2 arguments, not 0
args.d(11): no property 'va_start' for type 'void'

What am I doing wrong?

Thanks,
Ted
May 23 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Ted Williams wrote:

I have tried to use the templates in the std.c.stdarg package.  I cant get
the code to compile.
The following snippet illustrates the problem:

import std.c.stdarg;

void TestArgs( char[] format, ... )
{
    va_list args;

    alias va_start!( char[] ) s;
    alias va_arg!(Object) a;

    s.va_start( args, format );
    Object o = a.va_arg( args );
}

When compiled I get the following errors:

args.d(11): function va_start (va_list ap,char[]parmn) does not match
argument types ()
args.d(11): Error: expected 2 arguments, not 0
args.d(11): no property 'va_start' for type 'void'

What am I doing wrong?

Thanks,
Ted


  
void TestArgs( char[] format, ... ) { va_list args; alias va_start!( char[] ) s; alias va_arg!(Object) a; s( args, format ); Object o = a( args ); } -- -Anderson: http://badmama.com.au/~anderson/
May 23 2004
prev sibling next sibling parent Andrew Edwards <ridimz_at yahoo.dot.com> writes:
Ted Williams wrote:
 I have tried to use the templates in the std.c.stdarg package.  I cant get
 the code to compile.
 The following snippet illustrates the problem:
 
 import std.c.stdarg;
 
 void TestArgs( char[] format, ... )
 {
     va_list args;
 
     alias va_start!( char[] ) s;
     alias va_arg!(Object) a;
 
     s.va_start( args, format );
     Object o = a.va_arg( args );
 }
Try this instead: s( args, format ); Object o = a( args );
 When compiled I get the following errors:
 
 args.d(11): function va_start (va_list ap,char[]parmn) does not match
 argument types ()
 args.d(11): Error: expected 2 arguments, not 0
 args.d(11): no property 'va_start' for type 'void'
 
 What am I doing wrong?
 
 Thanks,
 Ted
 
 
May 23 2004
prev sibling next sibling parent J C Calvarese <jcc7 cox.net> writes:
Ted Williams wrote:
 I have tried to use the templates in the std.c.stdarg package.  I cant get
 the code to compile.
 The following snippet illustrates the problem:
 
 import std.c.stdarg;
 
 void TestArgs( char[] format, ... )
 {
     va_list args;
 
     alias va_start!( char[] ) s;
     alias va_arg!(Object) a;
 
     s.va_start( args, format );
     Object o = a.va_arg( args );
 }
 
 When compiled I get the following errors:
 
 args.d(11): function va_start (va_list ap,char[]parmn) does not match
 argument types ()
 args.d(11): Error: expected 2 arguments, not 0
 args.d(11): no property 'va_start' for type 'void'
 
 What am I doing wrong?
 
 Thanks,
 Ted
I don't know why your code doesn't compile. But here's an example that Walter posted earlier that does seem to work: import std.c.stdarg; int foo(char *x, ...) { va_list ap; va_start!(typeof(x))(ap, x); printf("&x = %p, ap = %p\n", &x, ap); int i; i = va_arg!(typeof(i))(ap); printf("i = %d\n", i); long l; l = va_arg!(typeof(l))(ap); printf("l = %lld\n", l); uint k; k = va_arg!(typeof(k))(ap); printf("k = %u\n", k); va_end(ap); return i + l + k; } void main() { int j; j = foo("hello", 3, 23L, 4); printf("j = %d\n", j); assert(j == 30); } /* adapted from http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/273 */ -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
May 23 2004
prev sibling next sibling parent Hauke Duden <H.NS.Duden gmx.net> writes:
Ted Williams wrote:
 I have tried to use the templates in the std.c.stdarg package.  I cant get
 the code to compile.
 The following snippet illustrates the problem:
 
 import std.c.stdarg;
 
 void TestArgs( char[] format, ... )
 {
     va_list args;
 
     alias va_start!( char[] ) s;
     alias va_arg!(Object) a;
 
     s.va_start( args, format );
     Object o = a.va_arg( args );
 }
You do realize that you don't HAVE to use aliases, don't you? D's templates have been upgraded a while back to allow convenient use without the need for those aliases. I'm just asking because lately I've seen aliases used a lot where they don't seem necessary. The following would work: void TestArgs( char[] format, ... ) { va_list args; va_start!(char[])(args, format ); Object o = va_arg!(Object)( args ); } Hauke
May 24 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Ted Williams wrote:

 I have tried to use the templates in the std.c.stdarg package.  I cant get
 the code to compile.
<snip>
 What am I doing wrong?
IMHO, trying to do D stuff with a syntax feature that was presumably only put in for interfacing to C. (My guess is that the templates were put in there for the sake of completeness rather than anything else.) Mabye there are better solutions, or maybe it's better to wait for D to have its own, built-in support for VALs.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 24 2004