Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 27, 2018 Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation? | ||||
---|---|---|---|---|
| ||||
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)`? |
June 27, 2018 Re: Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan S. | 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
|
June 27, 2018 Re: Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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); } --- |
June 27, 2018 Re: Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan S. | 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
|
June 27, 2018 Re: Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan S. | 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
|
Copyright © 1999-2021 by the D Language Foundation