Thread overview
Function name from function pointer
Apr 11, 2015
Paul D Anderson
Apr 11, 2015
Marco Leise
Apr 11, 2015
Paul D Anderson
Apr 11, 2015
bearophile
April 11, 2015
Is there a way to return the name of a function (a string) from a pointer to that function?

Function pointer example from D Reference:
---
int function() fp;

void test()
{
    static int a = 7;
    static int foo() { return a + 3; }

    fp = &foo;
}

void bar()
{
    test();
    int i = fp();       // i is set to 10
}
---

Can I get "foo" from fp?

Paul


April 11, 2015
Am Sat, 11 Apr 2015 18:28:35 +0000
schrieb "Paul D Anderson" <claude.reins@msnmail.com>:

> Is there a way to return the name of a function (a string) from a pointer to that function?
> 
> Function pointer example from D Reference:
> ---
> int function() fp;
> 
> void test()
> {
>      static int a = 7;
>      static int foo() { return a + 3; }
> 
>      fp = &foo;
> }
> 
> void bar()
> {
>      test();
>      int i = fp();       // i is set to 10
> }
> ---
> 
> Can I get "foo" from fp?
> 
> Paul
> 
> 

Nope, that would require that fp not only contains a pointer to the function but also a pointer to the name. That's not how it works. But continuing that thought, you could add the function's name as an additional variable and set that every time you set fp.

-- 
Marco

April 11, 2015
Paul D Anderson:

> Is there a way to return the name of a function (a string) from a pointer to that function?

Perhaps creating a string[void*] AA and initializing with all the function pointers you care about.

Bye,
bearophile
April 11, 2015
On Saturday, 11 April 2015 at 19:08:50 UTC, Marco Leise wrote:
> Am Sat, 11 Apr 2015 18:28:35 +0000
> schrieb "Paul D Anderson" <claude.reins@msnmail.com>:
>
>> Is there a way to return the name of a function (a string) from a pointer to that function?
>> 
>> Function pointer example from D Reference:
>> ---
>> int function() fp;
>> 
>> void test()
>> {
>>      static int a = 7;
>>      static int foo() { return a + 3; }
>> 
>>      fp = &foo;
>> }
>> 
>> void bar()
>> {
>>      test();
>>      int i = fp();       // i is set to 10
>> }
>> ---
>> 
>> Can I get "foo" from fp?
>> 
>> Paul
>> 
>> 
>
> Nope, that would require that fp not only contains a pointer
> to the function but also a pointer to the name. That's not how
> it works. But continuing that thought, you could add the
> function's name as an additional variable and set that every
> time you set fp.

Okay, thanks, I can see that.

Paul