44

I'm trying to define a class method for debug prints that will behave like printf:

inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)))

When I compile with -Wformat or -Wall, This complains about:

error: format string argument not a string type

I recalled that a class method declaration has an implicit this parameter, so I changed the locations of the parameters to 2, 3:

inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 2, 3)))

and now it compiles, but it looks like the parameters are shifted, as if the this parameter were being treated as part of the argument list.

How can I tell the function that this isn't part of the string that I want to print?

6
  • Can you use variadic templates? If so, you can make a type safe printf
    – chris
    Jul 23, 2012 at 21:48
  • 2
    Don't think too much about this. It's not an explicit argument, period. Just follow the GCC manual, which says that for member functions you have to add 1 to the format-attribute arguments. It's just an opaque rule, given to you by the vendor of a compiler extension.
    – Kerrek SB
    Jul 23, 2012 at 21:50
  • 3
    printf (2, 3) is right. Define "seems like shifted"...?
    – user405725
    Jul 23, 2012 at 21:51
  • The format attribute specifies style of the format string, the argument location of the format string, and the argument location of the .... The compiler then uses that information to do type checking of the ... arguments with the format string.
    – jxh
    Jul 23, 2012 at 22:09
  • Uh! And even with gcc 6.2 the error is still as cryptic... Oct 3, 2016 at 15:59

4 Answers 4

44

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.

2
  • It makes sense but I wonder why this is not documented anywhere
    – dashesy
    Jun 15, 2017 at 17:14
  • 9
    you are right here: 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 and first-to-check.
    – dashesy
    Jun 16, 2017 at 18:14
6

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...

5

@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 and first-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):

enter image description here

2

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
3
  • 2
    That's a good point in general, but it's not an issue for me, as this is an internal code base built using a strict flow. Sep 9, 2013 at 6:55
  • 6
    You could also use #ifndef __GNUC__ #define __attribute__(a). Then you can use any attribute.
    – cubuspl42
    Apr 17, 2014 at 18:03
  • 2
    It works for clang too, actually, which is, by design, gcc compatible. "It is designed to act as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions." (Wikipedia). Apr 28, 2020 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.