digitalmars.D - wrapping functions with variadic-parameter wrappers
- Gor Gyolchanyan (21/21) Dec 05 2012 What's the best (least overhead and most portable and safe) way wrapping...
- Jacob Carlborg (9/27) Dec 05 2012 It might not be want you need but a variadic template is the easiest
- Gor Gyolchanyan (13/47) Dec 05 2012 A function with variadic template parameters is just a function which ta...
- Mike Wey (13/25) Dec 05 2012 Something like this?
- Gor Gyolchanyan (5/35) Dec 05 2012 --
- Jonathan M Davis (6/17) Dec 05 2012 Then read the section on variadic functions and pick which type works be...
- Gor Gyolchanyan (12/32) Dec 05 2012 Sorry, force of habit. Gmail puts me at the top and hides the message by
- Jonathan M Davis (9/15) Dec 06 2012 I think that at this point, almost everyone just uses variadic templates...
- Gor Gyolchanyan (10/29) Dec 06 2012 I know, that variadic templates are safer, but this use case requires th...
What's the best (least overhead and most portable and safe) way wrapping a function into a variadic-parameter function? For instance: long foo(int i, char c) { /// ... } long bar(...) { return foo(/* ??? */); } This is necessary for losing the parameter types, but still having the function callable with its expected parameters. I suspect this could be done using inline assembler and knowledge of the D ABI to achieve near-zero overhead. The wrapper can include dynamic type checking using the automatically passed _arguments array to ensure type safety. This is useful for dynamic dispatching. -- Bye, Gor Gyolchanyan.
Dec 05 2012
On 2012-12-05 11:01, Gor Gyolchanyan wrote:What's the best (least overhead and most portable and safe) way wrapping a function into a variadic-parameter function? For instance: long foo(int i, char c) { /// ... } long bar(...) { return foo(/* ??? */); } This is necessary for losing the parameter types, but still having the function callable with its expected parameters. I suspect this could be done using inline assembler and knowledge of the D ABI to achieve near-zero overhead. The wrapper can include dynamic type checking using the automatically passed _arguments array to ensure type safety. This is useful for dynamic dispatching.It might not be want you need but a variadic template is the easiest solution: long bar (Args ...) (Args args) { return foo(args); } -- /Jacob Carlborg
Dec 05 2012
A function with variadic template parameters is just a function which takes a set of compile-time known parameters. My goal is to have a non-template function taking variadic parameters. long bar(...) { return foo(...); } This is necessary to be able to pass variables to functions without knowing the type of the functions. On Wed, Dec 5, 2012 at 7:33 PM, Jacob Carlborg <doob me.com> wrote:On 2012-12-05 11:01, Gor Gyolchanyan wrote:-- Bye, Gor Gyolchanyan.What's the best (least overhead and most portable and safe) way wrapping a function into a variadic-parameter function? For instance: long foo(int i, char c) { /// ... } long bar(...) { return foo(/* ??? */); } This is necessary for losing the parameter types, but still having the function callable with its expected parameters. I suspect this could be done using inline assembler and knowledge of the D ABI to achieve near-zero overhead. The wrapper can include dynamic type checking using the automatically passed _arguments array to ensure type safety. This is useful for dynamic dispatching.It might not be want you need but a variadic template is the easiest solution: long bar (Args ...) (Args args) { return foo(args); } -- /Jacob Carlborg
Dec 05 2012
On 12/05/2012 04:40 PM, Gor Gyolchanyan wrote:A function with variadic template parameters is just a function which takes a set of compile-time known parameters. My goal is to have a non-template function taking variadic parameters. long bar(...) { return foo(...); } This is necessary to be able to pass variables to functions without knowing the type of the functions. -- Bye, Gor Gyolchanyan.Something like this? long bar(...) { if( _arguments[0] == typeid(int) && _arguments[1] == typeid(char) ) { return foo(va_arg!(int)(_argptr), va_arg!(char)(_argptr)); } else throw; } -- Mike Wey
Dec 05 2012
kinda, but with minimal overhead and boilerplate On Wed, Dec 5, 2012 at 8:32 PM, Mike Wey <mike-wey example.com> wrote:On 12/05/2012 04:40 PM, Gor Gyolchanyan wrote:-- Bye, Gor Gyolchanyan.A function with variadic template parameters is just a function which takes a set of compile-time known parameters. My goal is to have a non-template function taking variadic parameters. long bar(...) { return foo(...); } This is necessary to be able to pass variables to functions without knowing the type of the functions. -- Bye, Gor Gyolchanyan.Something like this? long bar(...) { if( _arguments[0] == typeid(int) && _arguments[1] == typeid(char) ) { return foo(va_arg!(int)(_argptr), va_arg!(char)(_argptr)); } else throw; } -- Mike Wey
Dec 05 2012
On Wednesday, December 05, 2012 19:40:44 Gor Gyolchanyan wrote:A function with variadic template parameters is just a function which takes a set of compile-time known parameters. My goal is to have a non-template function taking variadic parameters. long bar(...) { return foo(...); } This is necessary to be able to pass variables to functions without knowing the type of the functions.Then read the section on variadic functions and pick which type works best for you: http://dlang.org/function.html - Jonathan M Davis P.S. Please stop top posting. It's generally considered rude in lists like this.
Dec 05 2012
On Thu, Dec 6, 2012 at 10:02 AM, Jonathan M Davis <jmdavisProg gmx.com>wrote:On Wednesday, December 05, 2012 19:40:44 Gor Gyolchanyan wrote:Sorry, force of habit. Gmail puts me at the top and hides the message by default, I read about the d-style variadic functions and I also read the ABI. There are some vast differences in the ABI (the parameters are pushed in reverse order, the caller must clean the stack...). I thought that you guys have a better understanding of what's going on, so you might help me with this. If done carefully it could be a very valuable addition to std.functional. -- Bye, Gor Gyolchanyan.A function with variadic template parameters is just a function whichtakesa set of compile-time known parameters. My goal is to have a non-template function taking variadic parameters. long bar(...) { return foo(...); } This is necessary to be able to pass variables to functions withoutknowingthe type of the functions.Then read the section on variadic functions and pick which type works best for you: http://dlang.org/function.html - Jonathan M Davis P.S. Please stop top posting. It's generally considered rude in lists like this.
Dec 05 2012
On Thursday, December 06, 2012 11:40:24 Gor Gyolchanyan wrote:I read about the d-style variadic functions and I also read the ABI. There are some vast differences in the ABI (the parameters are pushed in reverse order, the caller must clean the stack...). I thought that you guys have a better understanding of what's going on, so you might help me with this. If done carefully it could be a very valuable addition to std.functional.I think that at this point, almost everyone just uses variadic templates when they want a variadic function. Upon occasion, a typesafe variadic function makes sense, but C style variadics basically never make sense unless you're interacting with C, and in general, variadic templates are vastly superior to D variadics, so it's the variadic templates get used. And as most of just use the variadic templates, in the case of many of us, our knowledge of D style variadics is low. - Jonathan M Davis
Dec 06 2012
I know, that variadic templates are safer, but this use case requires that the parameter types and quantities should be statically unknown. It's all part of dynamic dispatch that I'm implementing. For all we know they might interact with C. As far as I read on dlang.orgit will require tons of ASM to adapt variadic ABI to non-variadic ABI. On Thu, Dec 6, 2012 at 1:14 PM, Jonathan M Davis <jmdavisProg gmx.com>wrote:On Thursday, December 06, 2012 11:40:24 Gor Gyolchanyan wrote:-- Bye, Gor Gyolchanyan.I read about the d-style variadic functions and I also read the ABI. There are some vast differences in the ABI (the parameters are pushed in reverse order, the caller must clean the stack...). I thought that you guys have a better understanding of what's going on,soyou might help me with this. If done carefully it could be a very valuable addition to std.functional.I think that at this point, almost everyone just uses variadic templates when they want a variadic function. Upon occasion, a typesafe variadic function makes sense, but C style variadics basically never make sense unless you're interacting with C, and in general, variadic templates are vastly superior to D variadics, so it's the variadic templates get used. And as most of just use the variadic templates, in the case of many of us, our knowledge of D style variadics is low. - Jonathan M Davis
Dec 06 2012