I\'m trying to define a class method for debug prints that will behave like printf
:
inline void debug(const char* fmt, ...) __attribute__ ((form
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
@Chris Dodd is correct. Here's the latest gcc documentation to back it up (thanks Foxit reader for letting me mark up PDFs on Linux). Pay special attention to the part marked in green in the image below.
Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for
string-index
andfirst-to-check
.
Source: https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes (see the section titled "format (archetype, string-index, first-to-check)").
Image (esp. see highlighting in green):
Treat static members the same as non-members. The discussion gave me the answer, but it's worth noting for others:
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...
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.