c++ - VSCode错误: "incorrect use of va_start"

标签 c++ visual-studio-code printf variadic-functions

VSCode 提示在以下当前工作函数中使用va_start

void _log(int level, const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);
    writelog(level, format, arglist);
}

经过搜索后,我发现了对标准的引用,该标准似乎表明 VSCode 是正确的,并且代码将导致未定义的行为。

18.10/3 ...The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

我试图找到其他人如何处理类似功能的示例。我发现了几个博客和几个代码示例重新创建 printf,使用与上面类似的实现。这些例子也是不正确的吗?

编写类似 printf 的函数而不导致未定义行为的正确方法是什么?

最佳答案

所以我也遇到了这个问题,在处理构建输出时它让我感到困惑。我相信这是一个错误(我在这里注册了一个错误: https://github.com/Microsoft/vscode-cpptools/issues/1720 )。

我发现使用特殊的预处理器逻辑进行 __INTELLISENSE__ 构建可能很糟糕(如果平台不能原生支持 Intellisense,则这是必要的,但 vscode 是跨平台的,所以......)。

解决方法如下:

#if __INTELLISENSE__
#undef va_start(arg, va)
#define va_start(arg, va)
#undef va_end(va)
#define va_end(va)
#undef va_copy(va0, va1)
#define va_copy(arg0, va1)
#define __INT_MAX__ 0x7fffffff
#endif

我对 __INT_MAX__ 的定义也有疑问。

如果你绝望了,这会解决这些错误。

希望有人能够找到一种实际的解决方案,或者至少是一种不需要自定义代码的解决方案。

谢谢, 阿德里安

关于c++ - VSCode错误: "incorrect use of va_start",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49288611/

相关文章:

c - 打印数组元素给出错误的输出

c++ - 是否可以将 "Prototype pattern"调整为右值引用?

go - 为每个 vscode 项目设置 `GOPATH`

c - 奇怪的 printf 输出

Java:将二维字符串数组打印为右对齐表

python - 在 VS Code 中本地调试多个 Python Azure 函数

python tesseract 获取没有 OCR 的行数

c++ - 面向对象设计——最简单的情况,但我还是很困惑!

c++ - C++ char* 比较如何工作?

linux - Ubuntu中vscode中的实时服务器未在Android手机中加载