Compiler.. Thing? __func__

jim lee

Well-known member
I found __func__ to be amazingly useful for for giving me functions names while debugging. I don't recall where I ran into it. My question : Is there something like this that'll tell me the class that this function was in? Because, when you inherit a class, you suddenly get lots of the same name functions with no real idea what class they are from.

Anyone?

Thanks!
 
You could always print 'this' as an address, in addition. The printf format is "%p". That will show which specific object. Of course, that's just some number. Maybe in the constructor you could print what the current object's address is, plus some other info. This way, you know what object goes to which address.
 
The short answer is no, because a class may be masquerading as another class due to inheritance (and all sorts of other tricky situations).

(The long answer is g++ has __PRETTY_FUNCTION__ which gives the entire current function signature and you have to come up with a way to pull just the name out of it at runtime.)
 
__FUNCTION__, __PRETTY_FUNCTION__, __FILE__, __LINE__, __DATE__ etc. are preprocessor-provided macros, as described here. Because they are defined/fixed at compile time, they are literal constant expressions (constexpr) provided by the preprocessor, not variables.
 
Code:
__FUNCTION__ = sub
>>> __PRETTY_FUNCTION__ = void a::sub(int) T:\T_Drive\tCode\Forum26\PrettyFunc\PrettyFunc.ino
     Aug 10 2026 @line #9     :: next line #10

Code:
char foo[200];
class a {
  public:
    void sub (int i)
    {
      Serial.printf("\n__FUNCTION__ = %s\n", __FUNCTION__);
      //Serial.printf("__PRETTY_FUNCTION__ = %s %s %s\n", __PRETTY_FUNCTION__, __FILE__, __DATE__ );
      snprintf(foo, 200, "__PRETTY_FUNCTION__ = %s %s\n\t %s @line #%d\t", __PRETTY_FUNCTION__, __FILE__, __DATE__, __LINE__);
      i = __LINE__;
      Serial.printf ( ">>> %s :: next line #%d\n", foo, i );
    }
};
void setup() {
  Serial.begin(1);
  a ax;
  ax.sub (0);
}
void loop() {
}
 
Back
Top