相关疑难解决方法(0)

如何获得printf样式的编译时警告或错误

我想写一个像printf这样的例程,而不是功能上的,但是我希望例程能够像printf一样编译检查特性.

例如,如果我有:

{
   int i;
   std::string s;
   printf("%d %d",i);
   printf("%d",s.c_str());
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨如下:

1 cc1plus: warnings being treated as errors
2 In function 'int main()':
3 Line 8: warning: too few arguments for format
4 Line 9: warning: format '%d' expects type 'int', but argument 2 has type 'const char*'
Run Code Online (Sandbox Code Playgroud)

代码示例

printf和co是编译器对待的特殊函数,还是有一些技巧可以让它在任何用户定义的函数上工作?我感兴趣的具体编译器是gcc和msvc

c++ compiler-construction printf typechecking

12
推荐指数
2
解决办法
5678
查看次数

标签 统计

c++ ×1

compiler-construction ×1

printf ×1

typechecking ×1