digitalmars.D.learn - Templating with Function Pointers?
- Maxime Chevalier-Boisvert (14/14) Dec 11 2013 I'd like to know if it's possible to create a templated function
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (31/35) Dec 11 2013 That one is pretty straightforward:
- Maxime Chevalier-Boisvert (3/43) Dec 11 2013 Thanks, I think the std.concurrency code demonstrates exactly
I'd like to know if it's possible to create a templated function that takes a function pointer as an argument, and uses static ifs to generate different behaviors based on the type signature of the function pointer. More specifically, I want to create code generation functions for my JIT compiler, and template them based on a pointer to a function that implements the semantics of the operation being jitted. Not all of these operations take the same arguments, so I'd like to be able to template and generate different machine code. Is it possible to template based on a function pointer argument? If so, is it possible to use static ifs to know the return type or the argument types of the function pointer's signature, as well as how many arguments it takes?
Dec 11 2013
On 12/11/2013 06:43 PM, Maxime Chevalier-Boisvert wrote:Is it possible to template based on a function pointer argument? If so, is it possible to use static ifs to know the return type or the argument types of the function pointer's signature, as well as how many arguments it takes?That one is pretty straightforward: import std.string; void temp(Func)(Func func) { static if (is (Func == int function(double))) { pragma(msg, "Found an int(double)"); } else static if (is (Func == ReturnT function(string, size_t), ReturnT)) { pragma(msg, format("This one returns %s", ReturnT.stringof)); } } int foo(double d) { return 42; } struct S {} S bar(string s, size_t st) { return S.init; } void main() { temp(&foo); temp(&bar); } It is also possible to use alias template parameters or variadic template parameters. You may want to look at std.concurrency's receive() function implementation for more ideas. Ali
Dec 11 2013
Thanks, I think the std.concurrency code demonstrates exactly what I need :) On Thursday, 12 December 2013 at 05:07:11 UTC, Ali Çehreli wrote:On 12/11/2013 06:43 PM, Maxime Chevalier-Boisvert wrote:Is it possible to template based on a function pointerargument? If so,is it possible to use static ifs to know the return type orthe argumenttypes of the function pointer's signature, as well as howmany argumentsit takes?That one is pretty straightforward: import std.string; void temp(Func)(Func func) { static if (is (Func == int function(double))) { pragma(msg, "Found an int(double)"); } else static if (is (Func == ReturnT function(string, size_t), ReturnT)) { pragma(msg, format("This one returns %s", ReturnT.stringof)); } } int foo(double d) { return 42; } struct S {} S bar(string s, size_t st) { return S.init; } void main() { temp(&foo); temp(&bar); } It is also possible to use alias template parameters or variadic template parameters. You may want to look at std.concurrency's receive() function implementation for more ideas. Ali
Dec 11 2013