Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 21, 2004 Variable argument lists | ||||
---|---|---|---|---|
| ||||
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 23, 2004 Re: Variable argument lists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ted Williams | "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++. |
December 01, 2008 Re: Variable argument lists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ted Williams Attachments: | 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
|
December 02, 2008 Re: Variable argument lists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fabian Claßen | Reply to Fabian, this thread is over 4 year old. most threads are in the digitalmars.d.* NG's |
Copyright © 1999-2021 by the D Language Foundation