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.

Community
  • 1
  • 1
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • Relates? http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c – SubSevn May 10 '11 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. – Thomas Matthews May 10 '11 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 '14 at 05: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::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 '14 at 05:57
  • 1
    Voting to reopen because C != C++. – Ciro Santilli OurBigBook.com Nov 21 '19 at 17:11

2 Answers2

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

vsekhar
  • 5,090
  • 5
  • 22
  • 23
  • 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 – NullVoxPopuli May 10 '11 at 18:56
  • main: 37 is the REGISTER_FUNCTION(func_pointer), the rest is in the header file. =\ – NullVoxPopuli May 10 '11 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.

Steve Fallows
  • 6,274
  • 5
  • 47
  • 67
  • `void foo(int x) { printf("Current function: %s(%d)\n", __FUNCTION__, x);` Will print out:"Current function: foo([parameter value])" – Dan F May 10 '11 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. – Steve Fallows May 10 '11 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 '11 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. – NullVoxPopuli May 10 '11 at 19:04
  • note that `C++11` does speficify `__func__` which is similar to `__FUNCTION__`. Maybe this answer needs an update. See also here: https://stackoverflow.com/questions/4384765/whats-the-difference-between-pretty-function-function-func – 463035818_is_not_an_ai Sep 10 '19 at 09:49