c++ - "Forwarding" string literals -
i dealing library has variadic macro meant used printf
#define printf_like (format, ...) //some statement expression
since printf_like
required evaluate something, , in order avoid usual if
, dangling else
issue macros having multiple statements, implemented using gcc's statement expressions. however, need code build intel compiler, doesn't allow destructible entities inside statement expression. means can't write code this:
printf_like("%s", getstring().c_str());
where getstring
returns std::string
. work around this, have simple variadic template wrapper.
template <typename ... rest> int callprintflike(const char* const format, rest... rest) { return printf_like(format, rest...);//warning: format string not string literal [-wformat-nonliteral] }
and use this:
callprintflike("%s", getstring().c_str());//warning shown in above comment
this trips clang , gcc's -wformat-nonliteral
warning. there way me somehow "forward" string-literalness , have warning triggered when callprintflike not called string literal?
edit : 1 of answers below suggested using __attribute__((format))
. however, that doesn't work since format
attribute needs variadic function.
i hacked around turning warnings off function, providing macro call through , re-enabling warnings here through dead-branch of ternary operator.
//compiler specific pragmas turn off -wformat-security , -wformat-literal warnings go here template <typename ... rest> int callprintflike(const char* const format, rest... rest) { return printf_like(format, rest...); } //pop diagnostic pragmas #define new_printf_like (format, ...) (true ? callprintflike(format, ##__va_args__) : printf(format, ##__va_args__))
in new_printf_like
, printf(format, ##__va_args__)
never executed. however, compile-time checking of printf
call , warnings non-literal format
, arguments not matching format string still enabled.
Comments
Post a Comment