c++ - [[弃用]] 导致 VS2017 中的构建失败(错误 C4996)

标签 c++ visual-studio-2017 deprecated cl.exe

[[deprecated]]
void foo(){};

int main()
{
    foo();
}

以上代码在VS2017中产生如下错误:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>deprecation.cpp
1>d:\source\deprecation.cpp(6): error C4996: 'foo': was declared deprecated
1>d:\source\deprecation.cpp(2): note: see declaration of 'foo'
1>Done building project "test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

不幸的是,搜索此错误会导致堆积如山的“如何使用 [[deprecated]]”和“哪些函数已被弃用?”风格问题。

我检查的每个资源似乎都建议 [[deprecated]] 的使用应该像上面写的一样简单 - 编译器错误/警告是预期的,但不是构建失败。注释掉 [[deprecated]] 行会导致构建成功。

项目的目标是 Windows SDK 版本 10.0.17763.0。

cl.exe 版本为 19.16.27027。

我在使用 [[deprecated]] 时是否遗漏了一些明显的东西,或者 VS2017 是否找到了打破它的方法?

最佳答案

您收到错误而不是警告,因为启用了“SDL 检查”。它看起来像默认启用的 VS17 SDL。来自 doc :

/sdl enables these warnings as errors:

C4146 / we4146 A unary minus operator was applied to an unsigned type, resulting in an unsigned result.

C4308 / we4308 A negative integral constant converted to unsigned type, resulting in a possibly meaningless result.

C4532 / we4532 Use of continue, break or goto keywords in a __finally / finally block has undefined behavior during abnormal termination.

C4533 / we4533 Code initializing a variable will not be executed.

C4700 / we4700 Use of an uninitialized local variable.

C4703 / we4703 Use of a potentially uninitialized local pointer variable.

C4789 / we4789 Buffer overrun when specific C run - time(CRT) functions are used.

C4995 / we4995 Use of a function marked with pragma deprecated.

C4996 / we4996 Use of a function marked as deprecated.

要修复,请转到“属性”->“C/C++”->“SDL 检查”,设置为“No(/sdl-)”。之后你会得到

: warning C4996: 'foo': was declared deprecated
: note: see declaration of 'foo'

关于c++ - [[弃用]] 导致 VS2017 中的构建失败(错误 C4996),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007377/

相关文章:

swift - MKDirections 请求源和目的地

c++ - 静态变量访问

c++ - 如何从 c++ 的一个方面(aspect c++)释放或删除函数中分配的对象?

c++ - 有什么方法可以从 Visual Studio 2017 中的 Catch C++ 测试中获取代码覆盖率指标?

c# - 在 Visual Studio 中的 C# 泛型类/方法上设置函数断点?

java - 不推荐使用 findPreference() 方法? - 安卓

google-chrome - Object.observe 在 chrome 50 中移除

c++ - 使用实验性/文件系统时 undefined reference

c++ - 如何将 QString 复制到 wchar_t 缓冲区

visual-studio-2017 - 在 VS2017 中不断出现加载错误 : Domain object