How should I properly use __attribute__ ((format (printf, x, y))) inside a class method in C++?

You've done it. this is argument 1, so by saying format(printf, 2, 3) you're telling the compiler that you're NOT printing this, you're printing argument 2 (fmt) with additional arguments past that.


Treat static members the same as non-members. The discussion gave me the answer, but it's worth noting for others:

  • non-member functions work with 1,2
  • static member functions work with 1,2
  • non-static member functions treat 'this' as #1, so need 2,3

I found this because we have some processes that use log helpers like this and 1 out of 4 was requiring __attribute__ (( format( printf, 2, 3 ) )) with the other three working well with __attribute__ (( format(printf, 1, 2) )) - turned out it was non-static...


Since it only works for gcc, it would be good to define it this way to avoid errors on other compilers.

#ifdef __GNUC__
          __attribute__ (( format( printf, 2, 3 ) ))
#endif