digitalmars.D.learn - constructed variadic call
- Erik Smith (9/9) May 02 2016 Is there way to construct an "argument pack" from a non-static
- Stefan Koch (3/12) May 02 2016 Yes there is.
- Erik Smith (4/17) May 03 2016 I'm not sure how to apply mixins here because they require
- Alex Parrill (11/20) May 03 2016 I don't think it's possible to call a vararg function whose
- Erik Smith (3/13) May 03 2016 That was fast :). The generation idea helps a little - thanks.
Is there way to construct an "argument pack" from a non-static array (like the switch below)? I need to transport a variadic call through a void*. switch (a.length) { case 1: foo(a[1]); break; case 2: foo(a[1], a[2]); break; case 3: foo(a[1], a[2], a[3]); break; ... }
May 02 2016
On Monday, 2 May 2016 at 18:22:52 UTC, Erik Smith wrote:Is there way to construct an "argument pack" from a non-static array (like the switch below)? I need to transport a variadic call through a void*. switch (a.length) { case 1: foo(a[1]); break; case 2: foo(a[1], a[2]); break; case 3: foo(a[1], a[2], a[3]); break; ... }Yes there is. Using a string mixin.
May 02 2016
On Monday, 2 May 2016 at 18:56:59 UTC, Stefan Koch wrote:On Monday, 2 May 2016 at 18:22:52 UTC, Erik Smith wrote:I'm not sure how to apply mixins here because they require compile time evaluation and a.length is runtime. Ideas? erikIs there way to construct an "argument pack" from a non-static array (like the switch below)? I need to transport a variadic call through a void*. switch (a.length) { case 1: foo(a[1]); break; case 2: foo(a[1], a[2]); break; case 3: foo(a[1], a[2], a[3]); break; ... }Yes there is. Using a string mixin.
May 03 2016
On Monday, 2 May 2016 at 18:22:52 UTC, Erik Smith wrote:Is there way to construct an "argument pack" from a non-static array (like the switch below)? I need to transport a variadic call through a void*. switch (a.length) { case 1: foo(a[1]); break; case 2: foo(a[1], a[2]); break; case 3: foo(a[1], a[2], a[3]); break; ... }I don't think it's possible to call a vararg function whose number of arguments is only known at runtime, for the same reasons it is impossible in C [1]. Your switch statement is probably the best you can do, other than rewriting the API to not use varargs (which, depending on what the function is doing, I would recommend). You can possibly use string mixins or static foreach to avoid repeating the case clauses. [1] Populating a va_list: http://stackoverflow.com/questions/988290/populating-a-va-list
May 03 2016
I don't think it's possible to call a vararg function whose number of arguments is only known at runtime, for the same reasons it is impossible in C [1]. Your switch statement is probably the best you can do, other than rewriting the API to not use varargs (which, depending on what the function is doing, I would recommend). You can possibly use string mixins or static foreach to avoid repeating the case clauses. [1] Populating a va_list: http://stackoverflow.com/questions/988290/populating-a-va-listThat was fast :). The generation idea helps a little - thanks. It works though and I can't use va_list in this case. erik
May 03 2016