我想写一个像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