Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: get current function pointer without knowing function name?

In Windows/MSVS/C++ I can get a function pointer by pointing to its name like this:

void foo()
{
    auto fooPtr = &foo;
}

But can I do the same thing without knowing the name of the function?

void foo()
{
    auto fnPtr = &thisFunction; //no
}

Use case: I want to define a macro I can put at the top of many functions which will declare a pointer to the function. Ex:

#define defFnPtr auto fnPtr = &thisFunction
void foo()
{
    defFnPtr;
}
void bar()
{
    defFnPtr;
}
like image 633
Tyson Avatar asked Jul 23 '26 01:07

Tyson


1 Answers

No, there is no way in standard C++ to get a pointer to the "current" function.

Best that you could do is perhaps to use meta programming: Write a program that generates the line auto fnPtr = &foo; into the source.

That said, I don't think that the goal is worth the effort.

like image 89
eerorika Avatar answered Jul 25 '26 15:07

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!