17

In C++, is there a way to get the function signature/name from it's pointer like this?

void test(float data) {}
cout << typeid(&test).name();

I want to use this data for logging.

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Max Frai
  • 52,556
  • 73
  • 182
  • 293
  • 3
    Possible duplicate of: http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application http://stackoverflow.com/questions/359237/why-does-c-not-have-reflection – clyfe Jun 20 '10 at 10:14
  • 1
    We must assume that you are using function pointer variables, because otherwise it would be quite simple to write it as `cout << "void test(float)";`? – UncleBens Jun 20 '10 at 10:15

5 Answers5

27

C++, get the name of the calling function via a pointer:

Option 1: roll your own function name recorder

If you want to resolve a "pointer to a function" to a "function name", you will need to create your own lookup table of all available functions, then compare your pointer address to the key in the lookup table, and return the name.

An implementation described here: https://stackoverflow.com/a/8752173/445131

Option 2: Use __func__

GCC provides this magic variable that holds the name of the current function, as a string. It is part of the C99 standard:

#include <iostream>
using namespace std;
void foobar_function(){
    cout << "the name of this function is: " << __func__ << endl;
}
int main(int argc, char** argv) {
    cout << "the name of this function is: " << __func__ << endl;
    foobar_function();
    return 0;
}

Output:

the name of this function is: main
the name of this function is: foobar_function

Notes:

__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. If maximum portability is required, we recommend you use __func__, but provide a fallback definition with the preprocessor, to define it if it is undefined:

 #if __STDC_VERSION__ < 199901L
 # if __GNUC__ >= 2
 #  define __func__ __FUNCTION__
 # else
 #  define __func__ "<unknown>"
 # endif
 #endif

Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

Community
  • 1
  • 1
Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
19

If you just want to log the current function name, most of the compilers have __FUNCTION__ macro, which will give you the current function name at compile time.

You may also look for stack walking techniques (here is an example for Windows), which can provide you more information about the current call stack and function names at runtime.

Jack Shainsky
  • 512
  • 4
  • 12
  • 3
    Isn't it `__func__`? http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html states that this is technically not a macro. – stefan Jul 08 '13 at 14:27
  • 9
    `__func__` is a standard one and it's required to be there; `__FUNCTION__` is probably some non-standard one. `__PRETTY_FUNCTION__` is worth mentioning, too. – Griwes Jul 08 '13 at 14:33
2

There's no way you can get the name of the function. Just because it doesn't reside inside the executable. It vanishes completely after your code is compiled & linked.

You may try renaming your functions/variables, and your executable will be the same (apart from mutable things the compiler may put, like build date/time, debug information ID, etc.)

Also try to open the executable file with some editor and look for the function name. Most likely you'll not find it.

You may however put some programmatic "decorations" that'll help you discover your function name at the runtime.

valdo
  • 11,718
  • 1
  • 34
  • 59
  • this is partly incorrect. Function names do reside in the executable/library if they're public. Otherwise they couldn't be called externally. Those names are called 'symbols' – FalcoGer Jul 29 '20 at 10:26
1

You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'. Check your compiler documentation for what the output of your code means. In g++ the output will be PFvfE, which I don't understand completely, but identifies a pointer (P) to a function (F) returning void (v) and taking a float (f) as single argument. Don't ask me what the E is...

(I don't have time to check the docs now, I just played with a sample program to guess that: print different function signatures)

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
0

I'm not 100% sure, but this seems to me like reflection (Java), and C++ does not support things like this. It may be that I just don't know,but i havent seen this for C++ yet.

InsertNickHere
  • 3,507
  • 3
  • 24
  • 23