4

I don’t know much about RTTI, but I believe that thanks to that you can retrieve the name of variables at run-time. Is it possible to retrieve the name of the function the thread is currently running ?

qdii
  • 11,918
  • 9
  • 56
  • 110
  • 1
    No, RTTI doesn't return the **name** of variables, it allows you to determine their **type**. – Cody Gray Jan 07 '12 at 13:06
  • 1
    possible duplicate of [How can one grab a stack trace in C?](http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c) – Šimon Tóth Jan 07 '12 at 13:07
  • Alright, I thought `typeid(var).name` was doing that but apparently it returns the name of the type of the variable. – qdii Jan 07 '12 at 13:08
  • Why would you even care? – fredoverflow Jan 07 '12 at 13:08
  • @FredOverflow: Debug information. – Xeo Jan 07 '12 at 13:10
  • It could be debug information. In my case I want to make wrapper functions: from a shared object A.so, I want a function foo to call the same function foo on another shared object B.so I would have opened. – qdii Jan 07 '12 at 13:15
  • @victor the result of `typeid(var).name` is implementation-defined. –  Jan 09 '12 at 12:59

4 Answers4

12

C++11 standardized __func__ for the current function.

Various compilers support variations of __FUNCTION__, __PRETTY_FUNCTION__, and others.

Xeo
  • 126,658
  • 49
  • 285
  • 389
2

If you're doing GNU compatible stuffs, you may want to try backtrace.

starrify
  • 13,651
  • 4
  • 33
  • 48
0

No.

C++'s run-time type identification allows you to figure out the type of an object, but not the name of the method you're currently in.

unwind
  • 378,987
  • 63
  • 458
  • 590
0

No, it is not possible. C++ does not support reflection (neither static nor dynamic) (like e.g. C#). You would need some preprocessor magic to emulate that.

Apart from that, there is not necessarily a notion of a function/method name during run-time (this only available as debugging information if you compiled your sources with the corresponding flags).

Andre
  • 1,559
  • 12
  • 25