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

[[deprecated]] results in build failure in VS2017 (Error C4996)

[[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 ==========

不幸的是,搜索此错误会导致堆积如山的 "how to use [[deprecated]]" 和 "which functions are deprecated?" 风格的问题。

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

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

cl.exe 版本为 19.16.27027.

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

您收到错误而不是警告,因为 "SDL checks" 已启用。它看起来像默认启用的 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.

要修复,请转到 "Properties" -> "C/C++" -> "SDL checks",设置为 "No(/sdl-)"。之后你会得到

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