D - varargs with void[]
- C. Sauls (24/24) Mar 28 2004 I remember vaguely Walter mentioning the use of void[] in D to
- C. Sauls (6/6) Mar 28 2004 Just to see if this would technically work at all, I tossed together
- Ben Hinkle (9/15) Mar 28 2004 I think that code will only work for data that fits inside
- J Anderson (4/20) Mar 28 2004 Why not simply make these things pointers?
- C. Sauls (6/30) Mar 29 2004 If you mean to make data of type void*[] instead of void[][] that won't
- J Anderson (11/43) Mar 29 2004 What about?
- C. Sauls (5/29) Mar 29 2004 Just tried rigging up a struct with two int fields and a char[] field.
I remember vaguely Walter mentioning the use of void[] in D to facilitate the same ability as void* in C... could a little syntax sugar be devised to support variable parameter lists using void[]? Maybe something like: void myprintf(char[] format, void[][] data) { int idx; char fc; char[] out; // ... switch (fc) { case 'd': out ~= .toString(cast(int) data[idx++]); break; // ... case 's': out ~= .toString(cast(char[]) data[idx++]); break; // ... } // ... } Just a thought. -C. Sauls -Invironz
Mar 28 2004
Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :) [Source code attached] -C. Sauls -Invironz
Mar 28 2004
On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com> wrote:Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).[Source code attached] -C. Sauls -Invironz
Mar 28 2004
Ben Hinkle wrote:On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com> wrote:Why not simply make these things pointers? -- -Anderson: http://badmama.com.au/~anderson/Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Mar 28 2004
If you mean to make data of type void*[] instead of void[][] that won't work... I tried it. Cannot (apparently) cast a char[] or a struct to void*, but can cast anything to void[] (with varying degrees of success). -C. Sauls -Invironz J Anderson wrote:Ben Hinkle wrote:On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com> wrote:Why not simply make these things pointers?Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Mar 29 2004
C. Sauls wrote:If you mean to make data of type void*[] instead of void[][] that won't work... I tried it. Cannot (apparently) cast a char[] or a struct to void*, but can cast anything to void[] (with varying degrees of success). -C. Sauls -InvironzWhat about? char [] string; void *tt = (void*)&string[0]; //Looses length void *t = (void*)string; struct a { } a A; void *t = (void*)&A; What do you mean?J Anderson wrote:-- -Anderson: http://badmama.com.au/~anderson/Ben Hinkle wrote:On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com> wrote:Why not simply make these things pointers?Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Mar 29 2004
Just tried rigging up a struct with two int fields and a char[] field. The ints work jut fine, the char[] is lost, so you have a point. -C. Sauls -Invironz Ben Hinkle wrote:On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com> wrote:Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).[Source code attached] -C. Sauls -Invironz
Mar 29 2004