www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to get the address of the function that would be used

reply Nathan S. <no.public.email example.com> writes:
Let's say there's a function template `doImpl` and `doImpl(x)` 
compiles thanks to IFTI. Is there any way to get the address of 
the function that would be called in `doImpl(x)`?
Jun 27 2018
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, June 27, 2018 22:34:46 Nathan S. via Digitalmars-d-learn 
wrote:
 Let's say there's a function template `doImpl` and `doImpl(x)`
 compiles thanks to IFTI. Is there any way to get the address of
 the function that would be called in `doImpl(x)`?
You could explicitly instantiate the function template and then take its address. - Jonathan M Davis
Jun 27 2018
parent reply Nathan S. <no.public.email example.com> writes:
On Wednesday, 27 June 2018 at 22:39:26 UTC, Jonathan M Davis 
wrote:
 You could explicitly instantiate the function template and then 
 take its address.
Explicitly instantiating the template can result in a function that may be behaviorally identical but have a different address. https://run.dlang.io/is/E9WroB --- auto foo(T)(const T x) { return x; } void main() { const int a; assert(&foo!int !is &foo!(const int)); // The addresses are different. foo(a); // If I look in the object file I can see this uses foo!int. assert(&foo!(typeof(a)) !is &foo!int); } ---
Jun 27 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, June 27, 2018 22:59:03 Nathan S. via Digitalmars-d-learn 
wrote:
 On Wednesday, 27 June 2018 at 22:39:26 UTC, Jonathan M Davis

 wrote:
 You could explicitly instantiate the function template and then
 take its address.
Explicitly instantiating the template can result in a function that may be behaviorally identical but have a different address. https://run.dlang.io/is/E9WroB --- auto foo(T)(const T x) { return x; } void main() { const int a; assert(&foo!int !is &foo!(const int)); // The addresses are different. foo(a); // If I look in the object file I can see this uses foo!int. assert(&foo!(typeof(a)) !is &foo!int); } ---
Well, instantiating a template with int is definitely going to give a different funcion than instantiating with const int would, since they're different instantiations. My guess as to why foo(a) gets inferred as foo!int rather than foo!(const int) is because you made the parameter explicitly const, so the compiler then decides that T is the type without const and thus infers it to be int. Certainly, the surest thing to do is to explictly instantiate the template and use the explicit instantiation when calling it. I don't know if it's actually possible to even get the explicit instantation that was used for foo(a), let alone get the address of the resulting function. typeof(foo(a)) gives the type of the result, not the type of the function. - Jonathan M Davis
Jun 27 2018
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/27/18 6:34 PM, Nathan S. wrote:
 Let's say there's a function template `doImpl` and `doImpl(x)` compiles 
 thanks to IFTI. Is there any way to get the address of the function that 
 would be called in `doImpl(x)`?
It's a good question! You may be able to use TemplateOf, TemplateArgsOf, and Instantiate to generate the actual instantiation. -Steve
Jun 27 2018