D - Variable argument lists
- Ted Williams (5/5) May 21 2004 I have a stupid question: how does D handle variable length argument lis...
- Walter (5/8) May 23 2004 lists?
- =?ISO-8859-15?Q?Fabian_Cla=DFen?= (55/63) Dec 01 2008 So I have a little Example here:
- Benjamin Shropshire (2/2) Dec 01 2008 Reply to Fabian,
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
"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 argumentlists?I've been over the D document and didn't see anything on it unless Imissedsomething.They work just like they do in C and C++.
May 23 2004
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, TedSo 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
Reply to Fabian, this thread is over 4 year old. most threads are in the digitalmars.d.* NG's
Dec 01 2008