views:

111

answers:

4

Hello, are there any ways to get function signature/name from it's pointer? Like:

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

I wan't to use this data for logging.

+1  A: 

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
+4  A: 

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
+2  A: 

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
+1  A: 

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