Question How to use GCC's printf format attribute with C++11 variadic templates? is similar to mine, but I made more progress on the way to a solution.
GCC 4.8.1. Leaving aside the (in)efficiency issue, take the following method
template <typename ... Ts>
__attribute__((format(printf, 1, 0)))
inline std::string stdprintf(const char *format, Ts ...ts) {
char buf[256];
snprintf(buf, 256, format, ts...);
return std::string(buf);
}
According to the documentation, I had to make put 0 and not 2 in format(printf, 1, 0))
because the printf is used in a variadic way. The numbers represent which argument to the function is the format string, and which is the first argument to be checked for consistency with the % format specifiers. The 0 results in much weaker checking.
However, after template expansion, the function isn't really variadic. Is it just an issue that gcc insists on looking at this before expansion?