格式字符串不是可变模板函数(C++)中的字符串文字



我有一个简单的函数,其中我得到一个错误:

格式字符串不是字符串文字(可能不安全([-Weror,-Wformat security]

我知道我可以制作一个C风格的变分函数并使用:

__attribute__((__format__ (__printf__, x, y)))

但我必须保持它的C++模板风格。

这个问题有什么解决办法吗?我试过了:

printf("%s", boost::str((boost::format(inputString) % ... % args)).c_str());

但其工作方式与CCD_ 1不同。编辑:它的工作原理不一样,因为我处理类型不同:

uint8_t a = 1;
printf("%s", boost::str(boost::format("%u") % a ).c_str()); //this wont print anything, because %u doesnt match uint_8t
printf("%u",a); //this will print 1
template <typename... Types>
inline static void log(const char* inputString, Types... args)
{
printf(inputString, args...);
}

好的,我认为这个问题的一般好的解决方案是上面在评论中写的,但对我的项目限制有效的是:

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat"
#endif
template <typename... Types>
void log(const char* inputString, Types... args)
{
printf(inputString, args...);
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif

最新更新