G++ 是否可以添加关于将来将被弃用的函数或构造函数的编译消息(不是警告或错误)?

G++ Is it possible to add a compilation message (not warning or error) about a function or constructor that will be deprecated in the future?

如标题所述,我想知道我是否可以将函数标记为“弃用”,或者只是在编译期间输出一条消息 不是 警告或错误。

我不想阻止当前运行的应用程序的编译,只是通知开发人员我们计划在未来弃用函数或构造函数(并且可能添加一条消息详细说明 何时它将被弃用),以便他们可以在有空时进行更改。

我知道

等功能
[[deprecated("message")]] foo();

但我找不到任何不会产生警告或错误的信息。

必须可用于 G++ v7.0 或更高版本(C++17 或更低版本)

您最好的选择是继续使用 [[deprecated("message")]]。毕竟这是标记弃用的官方方式。

为了在保持 -Werror 的同时防止警告导致构建错误,您可以将错误“降级”为警告。

这可以作为编译标志来完成:-Wno-error=deprecated-declarations

或者直接在代码中:

#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wdeprecated-declarations"

// Insert code that has non-fatal deprecation warnings

#pragma GCC diagnostic pop