3
[[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 是否找到了打破它的方法?

4

1 回答 1

8

由于启用了“SDL 检查”,您会收到错误而不是警告。它看起来像在默认情况下启用的 VS17 SDL。来自文档

/sdl将这些警告作为错误启用

C4146 / we4146 一元减号运算符应用于无符号类型,导致无符号结果。

C4308 / we4308 负整数常量转换为无符号类型,导致可能无意义的结果。

C4532 / we4532 在 __finally / finally 块中使用 continue、break 或 goto 关键字在异常终止期间具有未定义的行为。

C4533 / we4533 初始化变量的代码将不会被执行。

C4700 / we4700 使用未初始化的局部变量。

C4703 / we4703 使用可能未初始化的局部指针变量。

C4789 / we4789 使用特定 C 运行时 (CRT) 函数时缓冲区溢出。

C4995 / we4995 不推荐使用标有 pragma 的函数。

C4996 / we4996 使用标记为已弃用的功能。

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

: warning C4996: 'foo': was declared deprecated
: note: see declaration of 'foo'
于 2019-03-05T16:51:16.337 回答