www.digitalmars.com         C & C++   DMDScript  

D - Variable argument lists

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
next sibling parent "Walter" <newshound digitalmars.com> writes:
"Ted Williams" <ted.wil.no.spam verizon.net> wrote in message
news:c8llpv$tqc$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.
They work just like they do in C and C++.
May 23 2004
prev sibling parent reply =?ISO-8859-15?Q?Fabian_Cla=DFen?= <admin fabs-world.de> writes:
Ted Williams schrieb:
 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
 
 
So I have a little Example here: If the arguments are from the same type: import std.stdio; double add(double param[] ...) { double result = 0; foreach(double d; param) { result += d; } return result; } int main() { double zahl; zahl = add(2, 5.6, 7.8); writefln("%s", zahl); return 0; } This is a good example. ;) You call it typesafe. ;) Or if there are different types of arguments: import std.stdio; int main() { double zahl = add(1, 5.6, 41, "Hello"); writefln("%s", zahl); return 0; } double add(...) { double result = 0; for(int i=0; i < _arguments.length; i++) { if(_arguments[i] == typeid(double)) { double var = *cast(double*)_argptr; result += var; _argptr += double.sizeof; } else if(_arguments[i] == typeid(float)) { float var = *cast(float*)_argptr; result += var; _argptr += double.sizeof; } else if(_arguments[i] == typeid(int)) { int var = *cast(int*)_argptr; result += var; _argptr += int.sizeof; } else if(_arguments[i] == typeid(bool)) { _argptr += bool.sizeof; } else if(_arguments[i] == typeid(char[])) { char[] var = *cast(char[]*)_argptr; _argptr += var.sizeof; } } return result; } But you see: You have to read out the types of the arguments. You can use: _arguments[] for the arguments. But you have to do "_argptr += argumenttype.sizeof" because so you get the beginning of the next argument. Greetings Fabian Claßen
Dec 01 2008
parent Benjamin Shropshire <ao pathlink.com> writes:
Reply to Fabian,

this thread is over 4 year old. most threads are in the digitalmars.d.* NG's
Dec 01 2008