You'd end up defining a macro for portability, and writing code such as
void stringPrintf(std::string* out, FOLLY_PRINTF_FORMAT const char* format, ...)
FOLLY_PRINTF_FORMAT_ATTR(2, 3);
For definitions (static functions, etc that may not have declaration), GCC only supports FOLLY_PRINTF_FORMAT_ATTR
in the beginning, so there you end up with
FOLLY_PRINTF_FORMAT_ATTR(2, 3)
static void stringPrintf(std::string* out, FOLLY_PRINTF_FORMAT const char* format, ...)
{
///
}
The header is
// compiler specific attribute translation
// msvc should come first, so if clang is in msvc mode it gets the right defines
// NOTE: this will only do checking in msvc with versions that support /analyze
#ifdef _MSC_VER
#include <stddef.h>
#ifdef _USE_ATTRIBUTES_FOR_SAL
#undef _USE_ATTRIBUTES_FOR_SAL
#endif
/* nolint */
#define _USE_ATTRIBUTES_FOR_SAL 1
#include <sal.h> // @manual
#define FOLLY_PRINTF_FORMAT _Printf_format_string_
#define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
#else
#define FOLLY_PRINTF_FORMAT /**/
#define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
__attribute__((__format__(__printf__, format_param, dots_param)))
#endif
This works with MSVC, Clang, Clang on Windows, and GCC.
The macro used there comes from Facebook Folly, https://github.com/facebook/folly/blob/3a3a6d4fb673443f04536f2d385b9545ba135d7e/folly/Portability.h#L54