digitalmars.D.learn - Is there a way to get the address of the function that would be used
- Nathan S. (3/3) Jun 27 2018 Let's say there's a function template `doImpl` and `doImpl(x)`
- Jonathan M Davis (5/8) Jun 27 2018 You could explicitly instantiate the function template and then take its
- Nathan S. (17/19) Jun 27 2018 Explicitly instantiating the template can result in a function
- Jonathan M Davis (14/33) Jun 27 2018 Well, instantiating a template with int is definitely going to give a
- Steven Schveighoffer (5/8) Jun 27 2018 It's a good question!
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
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
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
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: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 DavisYou 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
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