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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With