4

I'm trying to combine gcc's compile-time checking of printf format strings with c++11's variadic template packs.

I know I can decorate a variadic function with gcc's __attribute__((format(__printf__, ...)) decorator.

I know I can expand a variadic template pack into a variadic function. eg: printf(fmt, pack...);

Is it possible to combine the two?

Working example of what I'm trying to achieve:

#include <iostream>

void check(const char*, ...) __attribute__((format(__printf__, 1, 2)));
void check(const char*, ...)
{ 
    // exists only for the purposes of catching bogus format strings
}

template<typename... Ts>
void check_t(const char fmt[], Ts... ts)
{
    // check the format string by expanding ts into the check function
    check(fmt, ts...);
}

int main()
{
    const char f[] = "foo %s";
    check_t(f, 5);              // compiles
    check(f, 5);                // fails to compile
    return 0;
}
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
  • @user657267 - the answer you linked talks about string literals. I'm using a `const char []` being passed to both `check_t` and `check` - so gcc can obviously tell the difference - I guess I'm wondering if I can have gcc look at the input into the function template in the same way. – Steve Lorimer Jul 22 '14 at 08:37

0 Answers0