3

Possible Duplicate:
How to get function's name from function's pointer in C?

I'm printing the configuration for the program to the command line each run, and one thing that I would like printed is the current hash function being used -- which is stored in a function pointer variable.

Is there a way to do something along the lines of:
std::cout << function_pointer.function_name_to_text() << "\n";

So like, if I have a function called sha1(char * pre_image), it would just output sha1 to the console? the entire method header would be grand too.

5
  • Relates? stackoverflow.com/questions/351134/…
    – SubSevn
    May 10, 2011 at 18:26
  • 2
    You could create your own function pointer class that requires function names. For function objects or functors this would be a good idea for debug releases. May 10, 2011 at 18:53
  • 1
    Marking and closing this question as duplicate with a similar question specific to the C language prevents someone from giving a C++ specific answer.
    – alfC
    Jan 24, 2014 at 5:54
  • 1
    since I can't post an answer I'll put my code here. It is system dependent and it works in Linux, with gcc 4.8.2 and the -rdynamic -ldl flags: template<class CFunction, typename = typename std::enable_if<std::is_function<typename std::remove_pointer<CFunction>::type>::value>::type> std::string function_arg_name(CFunction f){ Dl_info info; dladdr((void const*)f, &info); return demangle(info.dli_sname); }. (demangle is an obvious function based on abi::__cxa_demangle). You call it like this std::string fn = function_arg_name(&fun); //or ...(fun)
    – alfC
    Jan 24, 2014 at 5:57
  • 1
    Voting to reopen because C != C++. Nov 21, 2019 at 17:11

2 Answers 2

3

You can't get the function name at run-time since function names don't exist after compilation.

You can however build a separate lookup function that would save the name and associate it with the function pointer:

#include <iostream>
#include <map>
#include <string>
#include <boost/preprocessor/stringize.hpp>

typedef void (*fptr_t)(char*);
typedef std::map<fptr_t, std::string> function_map_t;
function_map_t fmap;

#define REGISTER_FUNCTION(f) fmap[f] = BOOST_PP_STRINGIZE(f);

void sha1(char*) {}  // a function you want to lookup

int main() {
      REGISTER_FUNCTION(sha1)
      fptr_t my_pointer = sha1;
      std::cout << "Function name is: " << fmap[my_pointer] << std::endl;
      return 0;
}

EDIT: Updated to compile

2
  • I got some errors:src/main.h:24: error: ISO C++ forbids declaration of ‘fptr_t’ with no type src/main.cpp: In function ‘int main(int, const char**)’: src/main.cpp:37: error: ‘f’ was not declared in this scope May 10, 2011 at 18:56
  • main: 37 is the REGISTER_FUNCTION(func_pointer), the rest is in the header file. =\ May 10, 2011 at 18:59
2

There is no way to do this in the C++ language as it does not support reflection. The __FUNCTION__ macro is a non-standard way to get the current (meaning what the compiler is now compiling) function name. It is built-in on many platforms, but not all. You might be able to use that to get close to what you want.

5
  • void foo(int x) { printf("Current function: %s(%d)\n", __FUNCTION__, x); Will print out:"Current function: foo([parameter value])"
    – Dan F
    May 10, 2011 at 18:36
  • No, and the more I think about it, I'm not sure how useful it is if you need the function name in a context where all you have is a pointer variable containing the function pointer. The table approach mentioned in the accepted answer of the linked dupe may be your best bet. My primary use for __FUNCTION__ is for logging/throwing errors. May 10, 2011 at 18:39
  • The C99 standard (and the upcoming C++ norm) offers static const char __func__[] as a predefined symbol in each function. This should work with more compilers (although __FUNCTION__ is quite common too).
    – Joan Rieu
    May 10, 2011 at 18:42
  • Since I'm calling the function hundreds of times... I only need to print the name of it once... like.. outside of the function that I need the name of. May 10, 2011 at 19:04
  • note that C++11 does speficify __func__ which is similar to __FUNCTION__. Maybe this answer needs an update. See also here: stackoverflow.com/questions/4384765/… Sep 10, 2019 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.