This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/18605] New: __attribute__ ((format (printf, ...))) doesn't work in C++ methods
- From: "mkaufmann at student dot ethz dot ch" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 22 Nov 2004 09:18:30 -0000
- Subject: [Bug c++/18605] New: __attribute__ ((format (printf, ...))) doesn't work in C++ methods
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
[This bug has already been reported in 1998 to the mailing list. Please look
there for more explanations: http://gcc.gnu.org/ml/gcc-bugs/1998-08/msg00434.html ]
__attribute__(format(printf, ...)) works for C procedures, but not for C++
methods. g++ counts the implicit "this" parameter, and therefore g++ only
accepts declarations with wrong numbers:
// This works, but should not:
void printMessage(const char* format, ...) __attribute__((format(printf,2,3)));
// This should work, but g++ prints the error "format string arg not a string type":
GCC bug workaround: Use __attribute__((format(printf,1,2)));
Example program:
#include <stdio.h>
#include <stdarg.h>
class Test
{
public:
void printMessage(const char* format, ...) __attribute__((format(printf,1,2)));
};
void Test::printMessage(const char* format, ...)
{
va_list ap;
va_start(ap, format);
::vprintf(format, ap);
va_end(ap);
}
int main (int argc, char* argv[])
{
long l = 1000;
Test test;
test.printMessage("This should give a warning if compiled with '-Wformat':
%d\n", l);
return 0;
}
--
Summary: __attribute__ ((format (printf, ...))) doesn't work in
C++ methods
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: mkaufmann at student dot ethz dot ch
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18605