This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
C++11 variadic function and format attribute
- From: Diego Ongaro <ongaro at cs dot stanford dot edu>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 16 Apr 2012 00:11:06 -0700
- Subject: C++11 variadic function and format attribute
Hi all,
I was working on some C++11 template hacks to be able to pass C++ objects to a
function wrapping printf. What I want to do looks like this, where convert
might do fancy things like create string representations of objects:
template<typename... Args>
void Printf(const char* format, Args&&... args)
{
printf(format, convert(std::forward<Args>(args))...);
}
However, it seems that this is incompatible with the
__attribute__((format(printf, 1, 2))) applied to printf.
When trying to call this function as
Printf("%d\n", 3);
I get the following errors:
In function 'void Printf(const char*, Args&& ...) [with Args = int]':
instantiated from here
warning: format not a string literal, argument types not checked
Unfortunately, the compiler believes my format string is not a string literal,
but it is! It's really important to me that the arguments are checked against
the format string at compile time, as this catches serious bugs sooner.
I tried this on gcc-4.4 through 4.7 (and clang 3.0), with the same result.
Is this a compiler bug? Is there a work-around for this problem?
Thanks,
Diego