Is there a way to get the address of the function that would be used in Implicit Function Template Instantiation?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jun 28 00:12:14 UTC 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:
> > 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



More information about the Digitalmars-d-learn mailing list