Tyson
Tyson

Reputation: 1238

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;
}

Upvotes: 3

Views: 872

Answers (1)

eerorika
eerorika

Reputation: 238311

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.

Upvotes: 2

Related Questions