1

Possible Duplicate:
Is it ever possible to get the current (member) function name in C++?

If given a function int func(args)in C or C++ is there a way to get the name, or even signature, of the function fromwith in the body of func

I would like to be able to do something like this:

void func(void) 
{
    printf("%s", funcinfo.sig);
}

and have the output be:

"void func(void)"

does anyone know of a way to do this?

Community
  • 1
  • 1
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
  • funcinfo.sig what is? a function? a variable? – Ivan Jan 09 '12 at 12:49
  • In C99 you have the identifier `__func__` with the name of the function ("func" in your example), no way to get function type or parameters. – pmg Jan 09 '12 at 12:49
  • 1
    For the C part: [Does C have `__func__` functionality for names of the arguments of a function?](http://stackoverflow.com/questions/905999/does-c-have-func-functionality-for-names-of-the-arguments-of-a-function) has the answer. – Xeo Jan 09 '12 at 12:50
  • @Xeo you are right, that was just what I was looking for. – Martin Kristiansen Jan 09 '12 at 12:52

2 Answers2

3

There is __PRETTY_FUNCTION__ predefined identifier with gcc:

void func(void) 
{
    printf("%s\n", __PRETTY_FUNCTION__);
}

it will print :

void func()

with gcc in C++

and

func

with gcc in C

Note that this identifier is a gcc extension. In Standard C, there is the predefined identifier __func__ but which prints only the function name (C or C++)

ouah
  • 142,963
  • 15
  • 272
  • 331
2

"predefined identifier" __func__ in C99 and C++11` for function name.

log0
  • 10,489
  • 4
  • 28
  • 62