www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templating with Function Pointers?

reply "Maxime Chevalier-Boisvert" <maximechevalierb gmail.com> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent "Maxime Chevalier-Boisvert" <maximechevalierb gmail.com> writes:
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 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